UNPKG

@opengis/admin

Version:

This project Softpro Admin

90 lines (69 loc) 3.63 kB
import { getTemplate, handlebarsSync, pgClients, getToken } from "@opengis/fastify-table/utils.js"; function dayOfTheYear(date) { const start = new Date(date.getFullYear(), 0, 0); const diff = (date - start) + ((start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000); const oneDay = 1000 * 60 * 60 * 24; const day = Math.floor(diff / oneDay); return day; } export default async function codeGenerator({ pg = pgClients.client, params = {}, user = {}, query = {}, }, reply) { const { token, column } = params; const data = query.data?.split?.(';') || []; if (!token || !column) { return reply.status(400).send('not enough params: token / column'); } if (!user?.uid) { return reply.status(401).send('access restricted: token'); } const tokenData = await getToken({ token, uid: user?.uid, json: 1 }) || {}; if (!tokenData?.form || !tokenData?.table) { return reply.status(401).send('token not allow'); } const loadTemplate = await getTemplate('form', tokenData.form); const schema = loadTemplate?.schema || loadTemplate; if (!schema) { return reply.status(404).send('form not found'); } if (!schema?.[column]?.template) { return reply.status(400).send('template not specified'); } const columnValue = data.find(el => el.startsWith(column))?.split('=')?.pop(); const loadTable = await getTemplate('table', tokenData.table); const table = loadTable?.table || tokenData.table; if (!pg.pk?.[table]) { return reply.status(404).send('table pk not found'); } const { count = 0 } = await pg.query( `select count(*) from ${table} where ${columnValue ? `${column}::text = '${columnValue}'` : 'true'} limit 1`, ).then(el => el.rows?.[0] || {}); const { NUMY = 0 } = await pg.query( `select ${column} as "NUMY" from ${table} where ${column} is not null and date_part('year', cdate) = $1 order by cdate desc limit 1`, [(new Date()).getFullYear()] ).then(el => el.rows?.[0] || {}); const date = new Date(); const template = schema[column].template.match(/NUM[M|Y] \d/g) .reduce((acc, curr) => acc.replace( curr.startsWith('{{{') ? `{{{${curr}}}}` : `{{${curr}}}`, handlebarsSync.compile(`{{paddingNumber value padding}}`)({ padding: curr.substring(4, curr.length)?.trim?.(), value: curr.startsWith('NUMY') ? NUMY : +count + 1 })), schema[column].template ); const result = handlebarsSync.compile(template)({ HH: date.getHours(), // hours 24h: 14:00 = 14 HH12: (date.getHours() + 24) % 12 || 12, // hours 12h: 14:00 = 2 HH24: date.getHours(), // hours 24h: 14:00 = 14 MI: date.getMinutes(), // minutes SS: date.getSeconds(), // seconds YYYY: date.getFullYear(), // full year: 2025 YY: date.getFullYear().toString().substring(2, 4), // last 2 digits of year: 25 MONTH: date.toLocaleString('en', { month: 'long' }).toUpperCase(), // month name: MARCH MON: date.toLocaleString('en', { month: 'long' }).substring(0, 3).toUpperCase(), // month name abbrev: MAR MM: date.getMonth() + 1, // month number: 1 - january, 12 - december D: date.getDay(), // day of the week: 1 - monday, 7 - sunday DD: date.getDate(), // day of the month: january 4 = 4 DDD: dayOfTheYear(date), // day of the year: march 4 = 63 ...data, }); return reply.status(200).send(result); }