UNPKG

@opengis/fastify-table

Version:

core-plugins

128 lines (127 loc) 5.04 kB
const insertSocialSQL = `insert into admin.users_social_auth(uid, phone, user_name, sur_name, email, social_auth_id, social_auth_type, social_auth_date, social_auth_obj, city, enabled) select $9, $1, $2, $3, $4, $5, $6, now(), $7, $8, true on conflict(social_auth_id,email) do update set phone=excluded.phone, user_name=excluded.user_name, sur_name=excluded.sur_name, email=excluded.email, social_auth_id=excluded.social_auth_id, social_auth_type=excluded.social_auth_type, social_auth_date=excluded.social_auth_date, social_auth_obj=excluded.social_auth_obj, city=excluded.city, enabled=excluded.enabled`; const insertUserSQL = `insert into admin.users (enabled, phone, user_name, sur_name, father_name, email, user_rnokpp) values(true, $1, $2, $3, $4, $5, $6) on conflict (user_rnokpp) do update set phone=excluded.phone, user_name=excluded.user_name, sur_name=excluded.sur_name, father_name=excluded.father_name, email=excluded.email returning uid`; const updateUserSQL = `update admin.users set phone=$1, user_name=$2, sur_name=$3, father_name=coalesce($4, father_name), email=$5, social_auth_id=$6, social_auth_type=$7 where uid=$8 returning uid`; const deleteSocialIdSQL = `delete from admin.users_social_auth where social_auth_id=$1 or email=$2`; import config from "../../../../config.js"; import logger from "../../logger/getLogger.js"; export default async function getQuery({ pg, data, }) { if (typeof data !== "object") { throw new Error("invalid param data"); } const { drfocode, // personal eusign code edrpoucode, // organization eusign code locality: city, middlename, email: emailOriginal, phone, sub, // google account ID } = data?.user || data || {}; // google and id.gov.ua compatibility const email = ["", "n/a"].includes(emailOriginal) ? null : emailOriginal; const name = data?.user ? data?.user?.given_name || data?.user?.givenname : data?.givenname; const surname = data?.user ? data?.user?.family_name || data?.user?.lastname : data?.lastname; const id = drfocode || edrpoucode || sub; if (!id && !email) { throw new Error("invalid params: no id or email"); } const authType = data?.type || "govid"; const isSocialPK = pg.pk?.["admin.users_social_auth"]; const userId = await pg .query(`select uid from admin.users where ${id ? "user_rnokpp=$1" : "email=$1"}`, [id || email]) .then((el) => el.rows?.[0]?.uid); const socialUserId = isSocialPK ? await pg .query(`select uid from admin.users_social_auth where $1 in (social_auth_id,email) limit 1`, [email || id]) .then((el) => el.rows?.[0]?.uid) : null; const client = await pg.connect(); try { await client.query("BEGIN"); if (userId || socialUserId) { // delete old user, prevent duplicates / access level issues await client.query("delete from admin.users where $1 in (user_rnokpp, social_auth_id) and uid<>$2", [id, userId || socialUserId]); } const uid = userId || socialUserId ? await client .query(updateUserSQL, [ phone, name, surname, middlename, email, id, authType, userId || socialUserId, ]) .then((el) => el.rows?.[0]?.uid) : await client .query(insertUserSQL, [phone, name, surname, middlename, email, id]) .then((el) => el.rows?.[0]?.uid); const args = [ phone, name, surname, email, id, authType, data?.user || data, city, uid, ]; if (isSocialPK) { await client.query(deleteSocialIdSQL, [id, email]); await client.query(insertSocialSQL, args); } await client.query("COMMIT"); logger.file("auth/getQuery", { data: config.debug ? data : undefined, id, email, name, surname, authType, uid, socialUserId, }); } catch (err) { await client.query("ROLLBACK"); logger.file("auth/getQuery/error", { error: err.toString(), data: config.debug ? data : undefined, id, email, userId, socialUserId, stack: err.stack, }); throw new Error("Помилка авторизації. Зверніться до адміністратора"); } finally { client.release(); } const result = await pg .query(`select * from admin.users where uid =(select uid from admin.users_social_auth where ${id ? "social_auth_id=$1" : "email=$1"} limit 1)`, [id || email]) .then((el) => el.rows?.[0] || {}); return result; }