@opengis/admin
Version:
This project Softpro Admin
137 lines (112 loc) • 6.23 kB
JavaScript
import { getMeta, getToken, pgClients } from '@opengis/fastify-table/utils.js';
const galleryExtList = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'mp4', 'mov', 'avi'];
const username = `coalesce(u.sur_name,'')||coalesce(' '||u.user_name,'') ||coalesce(' '||u.father_name,'')`;
/**
* Дістає CRM для widget
*
*/
export default async function widgetGet({
pg = pgClients.client, user = {}, params = {}, query = {}, unittest,
}, reply) {
const param = user?.uid ? await getToken({
token: params.objectid, mode: 'w', uid: user.uid,
}) : null;
const objectid = param ? JSON.parse(param)?.id : params.objectid;
if (!objectid) {
return reply.status(400).send('not enough params: id');
}
const sqls = {
comment: pg.pk['admin.users']
? `select communication_id, entity_id, body, subject, c.cdate, c.uid,
${username} as username, u.login, u.avatar
from crm.communications c
left join admin.users u on u.uid=c.uid
where entity_id=$1 order by cdate desc`
: 'select communication_id, entity_id, body, subject, cdate, uid from crm.communications where entity_id=$1 order by cdate desc',
history: `select * from (
SELECT change_id, entity_id, entity_type, change_type, change_date, a.change_user_id, a.uid, a.cdate, b.json_agg as changes,
${username} as username, u.login, u.avatar
FROM log.table_changes a
left join admin.users u on a.change_user_id = u.uid
left join lateral(
select json_agg(row_to_json(q)) from (
select change_data_id, entity_key, value_new, value_old from log.table_changes_data
where change_id=a.change_id
)q
)b on 1=1
where b.json_agg is not null and (entity_id=$1 or entity_id in (
select communication_id as comments from crm.communications where entity_id=$1
union all select checklist_id from crm.checklists where entity_id=$1)
)
union all
select change_id, entity_id, entity_type, change_type, change_date, a.change_user_id, a.uid, a.cdate, b.json_agg as changes,
${username} as username, u.login, u.avatar
FROM log.table_changes a
left join admin.users u on a.change_user_id = u.uid
left join lateral(
select json_agg(o) from (
select json_object_agg(entity_key, coalesce(value_new, value_old)) as o from log.table_changes_data
where change_id=a.change_id and entity_key not in ('uid', 'file_status', 'editor_id', 'cdate', 'editor_date', 'entity_id')
)q
)b on 1=1
where a.change_type in ('INSERT', 'DELETE') and a.entity_id in (select file_id from crm.files where entity_id=$1)
limit 100
)q order by cdate desc limit 100`,
checklist: pg.pk['admin.users']
? `SELECT checklist_id, entity_id, subject, is_done, done_date, c.uid, c.cdate, ${username} as username, u.login, u.avatar
FROM crm.checklists c
left join admin.users u on u.uid=c.uid
where entity_id=$1 order by cdate desc`
: 'SELECT checklist_id, entity_id, subject, is_done, done_date, uid, cdate FROM crm.checklists where entity_id=$1 order by cdate desc',
file: pg.pk['admin.users']
? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
${username} as username, u.login, isverified, u.avatar, u.uid as author, file_status
FROM crm.files c
left join admin.users u on u.uid=c.uid
where entity_id=$1 and file_status<>3 order by cdate desc`
: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, uid, cdate, file_type, ismain,
isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 order by cdate desc`,
gallery: pg.pk['admin.users']
? `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, c.ismain,
${username} as username, u.login, u.avatar, isverified, u.avatar, c.uid as author, file_status
FROM crm.files c
left join admin.users u on u.uid=c.uid
where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`
: `SELECT file_id, entity_id, entity_type, file_path, uploaded_name, ext, size, c.uid, c.cdate, file_type, ismain,
isverified, uid as author, file_status FROM crm.files c where entity_id=$1 and file_status<>3 and ext = any($2) order by cdate desc`,
};
const q = sqls[params.type];
if (!q) {
return reply.status(400).send('invalid widget type');
}
/* data */
const time = [Date.now()];
const { rows = [] } = await pg.query(q, [objectid, params.type === 'gallery' ? galleryExtList : null].filter((el) => el));
rows.forEach(row => Object.assign(row, { username: row.username?.trim?.() || row.login }));
time.push(Date.now());
/* Object info */
const { tableName } = pg.pk['log.table_changes'] ? await pg.query(
'select entity_type as "tableName" from log.table_changes where entity_id=$1 limit 1',
[]
).then(el => el.rows?.[0] || {}) : {};
const { pk, columns = [] } = await getMeta({ pg, table: tableName });
const authorIdColumn = columns.find(col => ['uid', 'created_by'].includes(col.name))?.name;
if (!pk && params.type === 'history' && !unittest) {
return reply.status(404).send('log table not found');
}
const q1 = `select ${username} as author, u.login, a.cdate, a.editor_date from ${tableName} a
left join admin.users u on a.${authorIdColumn}=u.uid where a.${pk}=$1 limit 1`;
const data = pg.pk['admin.users'] && pk && tableName ? await pg.query(q, [objectid, params.type === 'gallery' ? galleryExtList : null].filter((el) => el)).then(el => el.rows?.[0] || {}) : {};
if (query.debug && user?.user_type === 'admin') {
return {
q, type: params.type, q1, id: objectid, data,
};
}
return {
time: { data: time[1] - time[0] },
rows,
user: { uid: user?.uid, name: user?.user_name },
data: { author: data?.author, cdate: data?.cdate, edate: data?.editor_date },
objectid: params.objectid,
};
}