33371/backend/src/db/api/products.js
2025-08-13 14:54:33 +00:00

495 lines
12 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 ProductsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const products = await db.products.create(
{
id: data.id || undefined,
title: data.title || null,
description: data.description || null,
type: data.type || null,
accepts_cash: data.accepts_cash || false,
accepts_online_payment: data.accepts_online_payment || false,
location_address: data.location_address || null,
location_lat: data.location_lat || null,
location_lng: data.location_lng || null,
region: data.region || null,
social_website: data.social_website || null,
social_youtube: data.social_youtube || null,
social_instagram: data.social_instagram || null,
social_tiktok: data.social_tiktok || null,
social_facebook: data.social_facebook || null,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return products;
}
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 productsData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title || null,
description: item.description || null,
type: item.type || null,
accepts_cash: item.accepts_cash || false,
accepts_online_payment: item.accepts_online_payment || false,
location_address: item.location_address || null,
location_lat: item.location_lat || null,
location_lng: item.location_lng || null,
region: item.region || null,
social_website: item.social_website || null,
social_youtube: item.social_youtube || null,
social_instagram: item.social_instagram || null,
social_tiktok: item.social_tiktok || null,
social_facebook: item.social_facebook || null,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const products = await db.products.bulkCreate(productsData, {
transaction,
});
// For each item created, replace relation files
return products;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const products = await db.products.findByPk(id, {}, { transaction });
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.description !== undefined)
updatePayload.description = data.description;
if (data.type !== undefined) updatePayload.type = data.type;
if (data.accepts_cash !== undefined)
updatePayload.accepts_cash = data.accepts_cash;
if (data.accepts_online_payment !== undefined)
updatePayload.accepts_online_payment = data.accepts_online_payment;
if (data.location_address !== undefined)
updatePayload.location_address = data.location_address;
if (data.location_lat !== undefined)
updatePayload.location_lat = data.location_lat;
if (data.location_lng !== undefined)
updatePayload.location_lng = data.location_lng;
if (data.region !== undefined) updatePayload.region = data.region;
if (data.social_website !== undefined)
updatePayload.social_website = data.social_website;
if (data.social_youtube !== undefined)
updatePayload.social_youtube = data.social_youtube;
if (data.social_instagram !== undefined)
updatePayload.social_instagram = data.social_instagram;
if (data.social_tiktok !== undefined)
updatePayload.social_tiktok = data.social_tiktok;
if (data.social_facebook !== undefined)
updatePayload.social_facebook = data.social_facebook;
updatePayload.updatedById = currentUser.id;
await products.update(updatePayload, { transaction });
return products;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const products = await db.products.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of products) {
await record.update({ deletedBy: currentUser.id }, { transaction });
}
for (const record of products) {
await record.destroy({ transaction });
}
});
return products;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const products = await db.products.findByPk(id, options);
await products.update(
{
deletedBy: currentUser.id,
},
{
transaction,
},
);
await products.destroy({
transaction,
});
return products;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const products = await db.products.findOne({ where }, { transaction });
if (!products) {
return products;
}
const output = products.get({ plain: true });
output.event_details_post = await products.getEvent_details_post({
transaction,
});
output.help_details_post = await products.getHelp_details_post({
transaction,
});
output.images_post = await products.getImages_post({
transaction,
});
output.notifications_linked_product =
await products.getNotifications_linked_product({
transaction,
});
output.product_details_post = await products.getProduct_details_post({
transaction,
});
output.session_requests_linked_product =
await products.getSession_requests_linked_product({
transaction,
});
output.sessions_linked_post = await products.getSessions_linked_post({
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 = [];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike('products', 'title', filter.title),
};
}
if (filter.description) {
where = {
...where,
[Op.and]: Utils.ilike('products', 'description', filter.description),
};
}
if (filter.location_address) {
where = {
...where,
[Op.and]: Utils.ilike(
'products',
'location_address',
filter.location_address,
),
};
}
if (filter.region) {
where = {
...where,
[Op.and]: Utils.ilike('products', 'region', filter.region),
};
}
if (filter.social_website) {
where = {
...where,
[Op.and]: Utils.ilike(
'products',
'social_website',
filter.social_website,
),
};
}
if (filter.social_youtube) {
where = {
...where,
[Op.and]: Utils.ilike(
'products',
'social_youtube',
filter.social_youtube,
),
};
}
if (filter.social_instagram) {
where = {
...where,
[Op.and]: Utils.ilike(
'products',
'social_instagram',
filter.social_instagram,
),
};
}
if (filter.social_tiktok) {
where = {
...where,
[Op.and]: Utils.ilike(
'products',
'social_tiktok',
filter.social_tiktok,
),
};
}
if (filter.social_facebook) {
where = {
...where,
[Op.and]: Utils.ilike(
'products',
'social_facebook',
filter.social_facebook,
),
};
}
if (filter.location_latRange) {
const [start, end] = filter.location_latRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
location_lat: {
...where.location_lat,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
location_lat: {
...where.location_lat,
[Op.lte]: end,
},
};
}
}
if (filter.location_lngRange) {
const [start, end] = filter.location_lngRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
location_lng: {
...where.location_lng,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
location_lng: {
...where.location_lng,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true',
};
}
if (filter.type) {
where = {
...where,
type: filter.type,
};
}
if (filter.accepts_cash) {
where = {
...where,
accepts_cash: filter.accepts_cash,
};
}
if (filter.accepts_online_payment) {
where = {
...where,
accepts_online_payment: filter.accepts_online_payment,
};
}
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.products.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('products', 'title', query),
],
};
}
const records = await db.products.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,
}));
}
};