39516-vm/backend/src/db/api/service_offerings.js
2026-04-07 21:57:42 +00:00

601 lines
15 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 Service_offeringsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const service_offerings = await db.service_offerings.create(
{
id: data.id || undefined,
title: data.title
||
null
,
description: data.description
||
null
,
base_price: data.base_price
||
null
,
pricing_model: data.pricing_model
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await service_offerings.setProfessional( data.professional || null, {
transaction,
});
await service_offerings.setCategory( data.category || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.service_offerings.getTableName(),
belongsToColumn: 'gallery_images',
belongsToId: service_offerings.id,
},
data.gallery_images,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.service_offerings.getTableName(),
belongsToColumn: 'certificates',
belongsToId: service_offerings.id,
},
data.certificates,
options,
);
return service_offerings;
}
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 service_offeringsData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title
||
null
,
description: item.description
||
null
,
base_price: item.base_price
||
null
,
pricing_model: item.pricing_model
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const service_offerings = await db.service_offerings.bulkCreate(service_offeringsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < service_offerings.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.service_offerings.getTableName(),
belongsToColumn: 'gallery_images',
belongsToId: service_offerings[i].id,
},
data[i].gallery_images,
options,
);
}
for (let i = 0; i < service_offerings.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.service_offerings.getTableName(),
belongsToColumn: 'certificates',
belongsToId: service_offerings[i].id,
},
data[i].certificates,
options,
);
}
return service_offerings;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const service_offerings = await db.service_offerings.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.description !== undefined) updatePayload.description = data.description;
if (data.base_price !== undefined) updatePayload.base_price = data.base_price;
if (data.pricing_model !== undefined) updatePayload.pricing_model = data.pricing_model;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await service_offerings.update(updatePayload, {transaction});
if (data.professional !== undefined) {
await service_offerings.setProfessional(
data.professional,
{ transaction }
);
}
if (data.category !== undefined) {
await service_offerings.setCategory(
data.category,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.service_offerings.getTableName(),
belongsToColumn: 'gallery_images',
belongsToId: service_offerings.id,
},
data.gallery_images,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.service_offerings.getTableName(),
belongsToColumn: 'certificates',
belongsToId: service_offerings.id,
},
data.certificates,
options,
);
return service_offerings;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const service_offerings = await db.service_offerings.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of service_offerings) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of service_offerings) {
await record.destroy({transaction});
}
});
return service_offerings;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const service_offerings = await db.service_offerings.findByPk(id, options);
await service_offerings.update({
deletedBy: currentUser.id
}, {
transaction,
});
await service_offerings.destroy({
transaction
});
return service_offerings;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const service_offerings = await db.service_offerings.findOne(
{ where },
{ transaction },
);
if (!service_offerings) {
return service_offerings;
}
const output = service_offerings.get({plain: true});
output.professional = await service_offerings.getProfessional({
transaction
});
output.category = await service_offerings.getCategory({
transaction
});
output.gallery_images = await service_offerings.getGallery_images({
transaction
});
output.certificates = await service_offerings.getCertificates({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.users,
as: 'professional',
where: filter.professional ? {
[Op.or]: [
{ id: { [Op.in]: filter.professional.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.professional.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.service_categories,
as: 'category',
where: filter.category ? {
[Op.or]: [
{ id: { [Op.in]: filter.category.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.category.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'gallery_images',
},
{
model: db.file,
as: 'certificates',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'service_offerings',
'title',
filter.title,
),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike(
'service_offerings',
'description',
filter.description,
),
};
}
if (filter.base_priceRange) {
const [start, end] = filter.base_priceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
base_price: {
...where.base_price,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
base_price: {
...where.base_price,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.pricing_model) {
where = {
...where,
pricing_model: filter.pricing_model,
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
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,
},
};
}
}
}
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.service_offerings.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, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'service_offerings',
'title',
query,
),
],
};
}
const records = await db.service_offerings.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,
}));
}
};