582 lines
14 KiB
JavaScript
582 lines
14 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 PgsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pgs = await db.pgs.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name || null,
|
|
location: data.location || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await pgs.setPg_owner(data.pg_owner || null, {
|
|
transaction,
|
|
});
|
|
|
|
await pgs.setPgowner(data.pgowner || null, {
|
|
transaction,
|
|
});
|
|
|
|
await pgs.setTenants(data.tenants || [], {
|
|
transaction,
|
|
});
|
|
|
|
await pgs.setRooms(data.rooms || [], {
|
|
transaction,
|
|
});
|
|
|
|
await pgs.setPayments(data.payments || [], {
|
|
transaction,
|
|
});
|
|
|
|
await pgs.setExpenses(data.expenses || [], {
|
|
transaction,
|
|
});
|
|
|
|
return pgs;
|
|
}
|
|
|
|
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 pgsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name || null,
|
|
location: item.location || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const pgs = await db.pgs.bulkCreate(pgsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return pgs;
|
|
}
|
|
|
|
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 pgs = await db.pgs.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
if (data.location !== undefined) updatePayload.location = data.location;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await pgs.update(updatePayload, { transaction });
|
|
|
|
if (data.pg_owner !== undefined) {
|
|
await pgs.setPg_owner(
|
|
data.pg_owner,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.pgowner !== undefined) {
|
|
await pgs.setPgowner(
|
|
data.pgowner,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.tenants !== undefined) {
|
|
await pgs.setTenants(data.tenants, { transaction });
|
|
}
|
|
|
|
if (data.rooms !== undefined) {
|
|
await pgs.setRooms(data.rooms, { transaction });
|
|
}
|
|
|
|
if (data.payments !== undefined) {
|
|
await pgs.setPayments(data.payments, { transaction });
|
|
}
|
|
|
|
if (data.expenses !== undefined) {
|
|
await pgs.setExpenses(data.expenses, { transaction });
|
|
}
|
|
|
|
return pgs;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pgs = await db.pgs.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of pgs) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of pgs) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return pgs;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pgs = await db.pgs.findByPk(id, options);
|
|
|
|
await pgs.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await pgs.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return pgs;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const pgs = await db.pgs.findOne({ where }, { transaction });
|
|
|
|
if (!pgs) {
|
|
return pgs;
|
|
}
|
|
|
|
const output = pgs.get({ plain: true });
|
|
|
|
output.expenses_pg = await pgs.getExpenses_pg({
|
|
transaction,
|
|
});
|
|
|
|
output.payments_pg = await pgs.getPayments_pg({
|
|
transaction,
|
|
});
|
|
|
|
output.rooms_pg = await pgs.getRooms_pg({
|
|
transaction,
|
|
});
|
|
|
|
output.tenants_pg = await pgs.getTenants_pg({
|
|
transaction,
|
|
});
|
|
|
|
output.pg_owner = await pgs.getPg_owner({
|
|
transaction,
|
|
});
|
|
|
|
output.tenants = await pgs.getTenants({
|
|
transaction,
|
|
});
|
|
|
|
output.rooms = await pgs.getRooms({
|
|
transaction,
|
|
});
|
|
|
|
output.payments = await pgs.getPayments({
|
|
transaction,
|
|
});
|
|
|
|
output.expenses = await pgs.getExpenses({
|
|
transaction,
|
|
});
|
|
|
|
output.pgowner = await pgs.getPgowner({
|
|
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 userPgowner = (user && user.PGOwner?.id) || null;
|
|
|
|
if (userPgowner) {
|
|
if (options?.currentUser?.PGOwnerId) {
|
|
where.PGOwnerId = options.currentUser.PGOwnerId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.pg_owners,
|
|
as: 'pg_owner',
|
|
|
|
where: filter.pg_owner
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.pg_owner
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.pg_owner
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.pgowner,
|
|
as: 'pgowner',
|
|
|
|
where: filter.pgowner
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.pgowner
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
name: {
|
|
[Op.or]: filter.pgowner
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.tenants,
|
|
as: 'tenants',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.rooms,
|
|
as: 'rooms',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.payments,
|
|
as: 'payments',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.expenses,
|
|
as: 'expenses',
|
|
required: false,
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('pgs', 'name', filter.name),
|
|
};
|
|
}
|
|
|
|
if (filter.location) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('pgs', 'location', filter.location),
|
|
};
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.tenants) {
|
|
const searchTerms = filter.tenants.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.tenants,
|
|
as: 'tenants_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
first_name: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.rooms) {
|
|
const searchTerms = filter.rooms.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.rooms,
|
|
as: 'rooms_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
room_number: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.payments) {
|
|
const searchTerms = filter.payments.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.payments,
|
|
as: 'payments_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
amount: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.expenses) {
|
|
const searchTerms = filter.expenses.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.expenses,
|
|
as: 'expenses_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
description: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
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.PGOwnerId;
|
|
}
|
|
|
|
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.pgs.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('pgs', 'name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.pgs.findAll({
|
|
attributes: ['id', 'name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.name,
|
|
}));
|
|
}
|
|
};
|