585 lines
14 KiB
JavaScript
585 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 BlueprintsDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const blueprints = await db.blueprints.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title || null,
|
|
land_area: data.land_area || null,
|
|
floor_count: data.floor_count || null,
|
|
room_count: data.room_count || null,
|
|
hall_count: data.hall_count || null,
|
|
kitchen_count: data.kitchen_count || null,
|
|
drawing_room_count: data.drawing_room_count || null,
|
|
pooja_room_count: data.pooja_room_count || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await blueprints.setArchitect(data.architect || null, {
|
|
transaction,
|
|
});
|
|
|
|
await blueprints.setFirm(data.firm || null, {
|
|
transaction,
|
|
});
|
|
|
|
await blueprints.setFirms(data.firms || null, {
|
|
transaction,
|
|
});
|
|
|
|
return blueprints;
|
|
}
|
|
|
|
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 blueprintsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title || null,
|
|
land_area: item.land_area || null,
|
|
floor_count: item.floor_count || null,
|
|
room_count: item.room_count || null,
|
|
hall_count: item.hall_count || null,
|
|
kitchen_count: item.kitchen_count || null,
|
|
drawing_room_count: item.drawing_room_count || null,
|
|
pooja_room_count: item.pooja_room_count || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const blueprints = await db.blueprints.bulkCreate(blueprintsData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return blueprints;
|
|
}
|
|
|
|
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 blueprints = await db.blueprints.findByPk(id, {}, { transaction });
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
if (data.land_area !== undefined) updatePayload.land_area = data.land_area;
|
|
|
|
if (data.floor_count !== undefined)
|
|
updatePayload.floor_count = data.floor_count;
|
|
|
|
if (data.room_count !== undefined)
|
|
updatePayload.room_count = data.room_count;
|
|
|
|
if (data.hall_count !== undefined)
|
|
updatePayload.hall_count = data.hall_count;
|
|
|
|
if (data.kitchen_count !== undefined)
|
|
updatePayload.kitchen_count = data.kitchen_count;
|
|
|
|
if (data.drawing_room_count !== undefined)
|
|
updatePayload.drawing_room_count = data.drawing_room_count;
|
|
|
|
if (data.pooja_room_count !== undefined)
|
|
updatePayload.pooja_room_count = data.pooja_room_count;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await blueprints.update(updatePayload, { transaction });
|
|
|
|
if (data.architect !== undefined) {
|
|
await blueprints.setArchitect(
|
|
data.architect,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.firm !== undefined) {
|
|
await blueprints.setFirm(
|
|
data.firm,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
if (data.firms !== undefined) {
|
|
await blueprints.setFirms(
|
|
data.firms,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return blueprints;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const blueprints = await db.blueprints.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of blueprints) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of blueprints) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return blueprints;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const blueprints = await db.blueprints.findByPk(id, options);
|
|
|
|
await blueprints.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await blueprints.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return blueprints;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const blueprints = await db.blueprints.findOne({ where }, { transaction });
|
|
|
|
if (!blueprints) {
|
|
return blueprints;
|
|
}
|
|
|
|
const output = blueprints.get({ plain: true });
|
|
|
|
output.models_blueprint = await blueprints.getModels_blueprint({
|
|
transaction,
|
|
});
|
|
|
|
output.architect = await blueprints.getArchitect({
|
|
transaction,
|
|
});
|
|
|
|
output.firm = await blueprints.getFirm({
|
|
transaction,
|
|
});
|
|
|
|
output.firms = await blueprints.getFirms({
|
|
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 userFirms = (user && user.firms?.id) || null;
|
|
|
|
if (userFirms) {
|
|
if (options?.currentUser?.firmsId) {
|
|
where.firmsId = options.currentUser.firmsId;
|
|
}
|
|
}
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
{
|
|
model: db.users,
|
|
as: 'architect',
|
|
|
|
where: filter.architect
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.architect
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.architect
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
|
|
{
|
|
model: db.firms,
|
|
as: 'firm',
|
|
},
|
|
|
|
{
|
|
model: db.firms,
|
|
as: 'firms',
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('blueprints', 'title', filter.title),
|
|
};
|
|
}
|
|
|
|
if (filter.land_areaRange) {
|
|
const [start, end] = filter.land_areaRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
land_area: {
|
|
...where.land_area,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
land_area: {
|
|
...where.land_area,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.floor_countRange) {
|
|
const [start, end] = filter.floor_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
floor_count: {
|
|
...where.floor_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
floor_count: {
|
|
...where.floor_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.room_countRange) {
|
|
const [start, end] = filter.room_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
room_count: {
|
|
...where.room_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
room_count: {
|
|
...where.room_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.hall_countRange) {
|
|
const [start, end] = filter.hall_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
hall_count: {
|
|
...where.hall_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
hall_count: {
|
|
...where.hall_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.kitchen_countRange) {
|
|
const [start, end] = filter.kitchen_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
kitchen_count: {
|
|
...where.kitchen_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
kitchen_count: {
|
|
...where.kitchen_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.drawing_room_countRange) {
|
|
const [start, end] = filter.drawing_room_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
drawing_room_count: {
|
|
...where.drawing_room_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
drawing_room_count: {
|
|
...where.drawing_room_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.pooja_room_countRange) {
|
|
const [start, end] = filter.pooja_room_countRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
pooja_room_count: {
|
|
...where.pooja_room_count,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
pooja_room_count: {
|
|
...where.pooja_room_count,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.firm) {
|
|
const listItems = filter.firm.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
firmId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
if (filter.firms) {
|
|
const listItems = filter.firms.split('|').map((item) => {
|
|
return Utils.uuid(item);
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
firmsId: { [Op.or]: listItems },
|
|
};
|
|
}
|
|
|
|
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.firmsId;
|
|
}
|
|
|
|
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.blueprints.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('blueprints', 'title', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.blueprints.findAll({
|
|
attributes: ['id', 'title'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title,
|
|
}));
|
|
}
|
|
};
|