431 lines
10 KiB
JavaScript
431 lines
10 KiB
JavaScript
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Schedules_mDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const schedules_m = await db.schedules_m.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
criterion: data.criterion || null,
|
|
weight: data.weight || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await schedules_m.setRfp(data.rfp || null, {
|
|
transaction,
|
|
});
|
|
|
|
await schedules_m.setPartner_assignment(data.partner_assignment || null, {
|
|
transaction,
|
|
});
|
|
|
|
await schedules_m.setOrganization(currentUser.organization.id || null, {
|
|
transaction,
|
|
});
|
|
|
|
return schedules_m;
|
|
}
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const schedules_mData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
criterion: item.criterion || null,
|
|
weight: item.weight || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const schedules_m = await db.schedules_m.bulkCreate(schedules_mData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return schedules_m;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const schedules_m = await db.schedules_m.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.criterion !== undefined) updatePayload.criterion = data.criterion;
|
|
|
|
if (data.weight !== undefined) updatePayload.weight = data.weight;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await schedules_m.update(updatePayload, { transaction });
|
|
|
|
if (data.rfp !== undefined) {
|
|
await schedules_m.setRfp(
|
|
data.rfp,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.partner_assignment !== undefined) {
|
|
await schedules_m.setPartner_assignment(
|
|
data.partner_assignment,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.organization !== undefined) {
|
|
await schedules_m.setOrganization(
|
|
globalAccess ? data.organization : currentUser.organization.id,
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return schedules_m;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const schedules_m = await db.schedules_m.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of schedules_m) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of schedules_m) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return schedules_m;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const schedules_m = await db.schedules_m.findByPk(id, options);
|
|
|
|
await schedules_m.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await schedules_m.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return schedules_m;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const schedules_m = await db.schedules_m.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!schedules_m) {
|
|
return schedules_m;
|
|
}
|
|
|
|
const output = schedules_m.get({ plain: true });
|
|
|
|
output.rfp = await schedules_m.getRfp({
|
|
transaction,
|
|
});
|
|
|
|
output.partner_assignment = await schedules_m.getPartner_assignment({
|
|
transaction,
|
|
});
|
|
|
|
output.organization = await schedules_m.getOrganization({
|
|
transaction,
|
|
});
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(filter, globalAccess, options) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganization = (user && user.Organization?.id) || null;
|
|
|
|
if (userOrganization) {
|
|
if (options?.currentUser?.OrganizationId) {
|
|
where.OrganizationId = options.currentUser.OrganizationId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.rfps,
|
|
as: 'rfp',
|
|
|
|
where: filter.rfp
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.rfp
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
title: {
|
|
[Op.or]: filter.rfp
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.partners,
|
|
as: 'partner_assignment',
|
|
|
|
where: filter.partner_assignment
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.partner_assignment
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.partner_assignment
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.organization,
|
|
as: 'organization',
|
|
|
|
where: filter.organization
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.organization
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.organization
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.criterion) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('schedules_m', 'criterion', filter.criterion),
|
|
};
|
|
}
|
|
|
|
if (filter.weightRange) {
|
|
const [start, end] = filter.weightRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
weight: {
|
|
...where.weight,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
weight: {
|
|
...where.weight,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
if (globalAccess) {
|
|
delete where.OrganizationId;
|
|
}
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order:
|
|
filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log,
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.schedules_m.findAndCountAll(
|
|
queryOptions,
|
|
);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count,
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(
|
|
query,
|
|
limit,
|
|
offset,
|
|
globalAccess,
|
|
organizationId,
|
|
) {
|
|
let where = {};
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike('schedules_m', 'criterion', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.schedules_m.findAll({
|
|
attributes: ['id', 'criterion'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['criterion', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.criterion,
|
|
}));
|
|
}
|
|
};
|