UNPKG

synt_backend

Version:

Synt light-weight node backend service

224 lines (202 loc) 6.42 kB
const db = require("./../mysql/models/index"); const translations = (t) => ({ synt_viewer: t("api.userTypes.syntViewer", "Synt Read-Only"), synt_authoriser: t("api.userTypes.syntAuthoriser", "Synt Official"), commissioner: t("api.userTypes.commissioner", "Auditor"), regular_user: t("api.userTypes.regularUser", "User"), bookkeeper: t("api.userTypes.bookkeeper", "Bookkeeper"), }); export const formatUser = (User, t) => { User.setDataValue("type", User.getDataValue("UserVME").type); User.setDataValue("role", translations(t)[User.getDataValue("UserVME").type]); return User; }; export const formatVME = (VME, t) => { // get company details if (VME) { VME.setDataValue("name", VME.Company?.name ?? VME.alias ?? ""); VME.setDataValue("address", (VME.Company && VME.Company.address) || ""); VME.setDataValue( "vat_number", (VME.Company && VME.Company.vat_number) || "" ); VME.setDataValue( "current_account", (VME.Company && VME.Company.current_account) || "" ); VME.setDataValue( "savings_account", (VME.Company && VME.Company.savings_account) || "" ); VME.setDataValue("email", (VME.Company && VME.Company.email) || ""); VME.setDataValue("phone", (VME.Company && VME.Company.phone) || ""); // format synt user data let Users = []; if (VME.Users) { VME.Users.forEach((User) => { if (User.UserVME.type === "synt_authoriser") VME.setDataValue("synt_name", User.full_name || ""); User = formatUser(User, t); Users.push(User); }); } VME.setDataValue("Users", Users); } return VME; }; export const formatSupplier = (Supplier) => { if (Supplier) { Supplier.setDataValue( "name", Supplier.Company?.name || Supplier.alias || "" ); Supplier.setDataValue("address", Supplier.Company?.address || ""); Supplier.setDataValue("vat_number", Supplier.Company?.vat_number || ""); Supplier.setDataValue("email", Supplier.Company?.email || ""); Supplier.setDataValue("phone", Supplier.Company?.phone || ""); Supplier.setDataValue( "current_account", Supplier.Company?.current_account || "" ); } return Supplier; }; export const formatLot = (Lot) => { if (Lot) { // format users let LotPhases = []; Lot.LotPhases.forEach((P) => { let Users = []; P.Users.forEach((User) => { User.setDataValue("type", User.LotPhaseUser.type || ""); User.setDataValue( "is_commissioner", User.LotPhaseUser.is_commissioner || false ); Users.push(User); }); P.Users = Users; LotPhases.push(P); }); Lot.setDataValue("LotPhases", LotPhases); } return Lot; }; export const formatMeetingItem = (MeetingItem) => { let FixedMeetingItemId = MeetingItem.FixedMeetingItemId; if (FixedMeetingItemId) { MeetingItem.setDataValue("name", MeetingItem.FixedMeetingItem.name); MeetingItem.setDataValue( "description", MeetingItem.FixedMeetingItem.description ); MeetingItem.setDataValue( "is_vote_required", MeetingItem.FixedMeetingItem.is_vote_required ); MeetingItem.setDataValue("majority", MeetingItem.FixedMeetingItem.majority); MeetingItem.setDataValue("is_fixed", true); return MeetingItem; } else { return MeetingItem; } }; export const setMeetingItemResults = (MeetingItem) => { let total_present_shares = 0; let total_pro = 0; let LotIds = []; MeetingItem.Users.forEach((User) => { User.LotPhases.forEach((LotPhase) => { let Lot = LotPhase.Lot; // FIXME: Only lots linked to this user at the date of the meeting if (!LotIds.includes(Lot.id)) { // is_commissioner / lasthebber -> voted so that's okay // geen onthouding => optellen if (User.MeetingItemUser.is_pro !== null) { total_present_shares = total_present_shares + Lot.share; } // optelling voor stemmen if (User.MeetingItemUser.is_pro) { total_pro = total_pro + Lot.share; } LotIds.push(Lot.id); } }); }); MeetingItem.total_shares = total_present_shares; MeetingItem.setDataValue("total_shares", total_present_shares); MeetingItem.total_pro = total_pro; MeetingItem.setDataValue("total_pro", total_pro); MeetingItem.passed = total_pro / total_present_shares >= MeetingItem.majority; MeetingItem.setDataValue( "passed", total_pro / total_present_shares >= MeetingItem.majority ); return MeetingItem; }; export const formatPowerOfAttorneys = (Meeting, VMEUsers) => { Meeting.Users = Meeting.Users.map((item) => { if (item.MeetingUser && item.MeetingUser.PowerOfAttorneyGivenToUserId) { item.MeetingUser.setDataValue( "PowerOfAttorneyGivenToUser", VMEUsers.find( (u) => u.id === item.MeetingUser.PowerOfAttorneyGivenToUserId ) ); } return item; }); return Meeting; }; export const findCommissioners = async (VME, includePast = false) => { const Lots = await VME.getLots({ include: { model: db.LotPhase, include: [ { model: db.User, attributes: { exclude: [ "phone", "email", "email_verified_at", "phone_verified_at", ], }, include: db.Company, }, ], }, }); const Commissioners = []; Lots.forEach((Lot) => { // current commissioner if includePast = false const last_periods = includePast ? Lot.LotPhases : Lot.LotPhases.filter((LP) => !LP.ends_at).slice(0, 1); const commissioners = last_periods .map((last_period) => last_period?.Users?.find((U) => U.LotPhaseUser.is_commissioner) ) .filter((c) => c != null); commissioners.forEach((Commissioner) => { // merge when exists let exists = false; Commissioners.forEach((C) => { if (C.id !== Commissioner.id) { return; } exists = true; C.setDataValue( "representing_shares", C.getDataValue("representing_shares") + Lot.share ); }); if (!exists) { Commissioner.setDataValue("representing_shares", Lot.share); Commissioners.push(Commissioner); } }); }); return Commissioners; };