39266-vm/backend/src/db/api/order_items.js
2026-03-22 14:49:54 +00:00

758 lines
18 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 Order_itemsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const order_items = await db.order_items.create(
{
id: data.id || undefined,
item_type: data.item_type
||
null
,
quantity: data.quantity
||
null
,
unit_price: data.unit_price
||
null
,
total_price: data.total_price
||
null
,
item_note: data.item_note
||
null
,
pizza_category: data.pizza_category
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await order_items.setOrder( data.order || null, {
transaction,
});
await order_items.setPizza_size( data.pizza_size || null, {
transaction,
});
await order_items.setStuffed_crust( data.stuffed_crust || null, {
transaction,
});
await order_items.setFlavor_primary( data.flavor_primary || null, {
transaction,
});
await order_items.setFlavor_secondary( data.flavor_secondary || null, {
transaction,
});
await order_items.setBeverage( data.beverage || null, {
transaction,
});
await order_items.setAdd_on( data.add_on || null, {
transaction,
});
return order_items;
}
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 order_itemsData = data.map((item, index) => ({
id: item.id || undefined,
item_type: item.item_type
||
null
,
quantity: item.quantity
||
null
,
unit_price: item.unit_price
||
null
,
total_price: item.total_price
||
null
,
item_note: item.item_note
||
null
,
pizza_category: item.pizza_category
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const order_items = await db.order_items.bulkCreate(order_itemsData, { transaction });
// For each item created, replace relation files
return order_items;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const order_items = await db.order_items.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.item_type !== undefined) updatePayload.item_type = data.item_type;
if (data.quantity !== undefined) updatePayload.quantity = data.quantity;
if (data.unit_price !== undefined) updatePayload.unit_price = data.unit_price;
if (data.total_price !== undefined) updatePayload.total_price = data.total_price;
if (data.item_note !== undefined) updatePayload.item_note = data.item_note;
if (data.pizza_category !== undefined) updatePayload.pizza_category = data.pizza_category;
updatePayload.updatedById = currentUser.id;
await order_items.update(updatePayload, {transaction});
if (data.order !== undefined) {
await order_items.setOrder(
data.order,
{ transaction }
);
}
if (data.pizza_size !== undefined) {
await order_items.setPizza_size(
data.pizza_size,
{ transaction }
);
}
if (data.stuffed_crust !== undefined) {
await order_items.setStuffed_crust(
data.stuffed_crust,
{ transaction }
);
}
if (data.flavor_primary !== undefined) {
await order_items.setFlavor_primary(
data.flavor_primary,
{ transaction }
);
}
if (data.flavor_secondary !== undefined) {
await order_items.setFlavor_secondary(
data.flavor_secondary,
{ transaction }
);
}
if (data.beverage !== undefined) {
await order_items.setBeverage(
data.beverage,
{ transaction }
);
}
if (data.add_on !== undefined) {
await order_items.setAdd_on(
data.add_on,
{ transaction }
);
}
return order_items;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const order_items = await db.order_items.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of order_items) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of order_items) {
await record.destroy({transaction});
}
});
return order_items;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const order_items = await db.order_items.findByPk(id, options);
await order_items.update({
deletedBy: currentUser.id
}, {
transaction,
});
await order_items.destroy({
transaction
});
return order_items;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const order_items = await db.order_items.findOne(
{ where },
{ transaction },
);
if (!order_items) {
return order_items;
}
const output = order_items.get({plain: true});
output.order = await order_items.getOrder({
transaction
});
output.pizza_size = await order_items.getPizza_size({
transaction
});
output.stuffed_crust = await order_items.getStuffed_crust({
transaction
});
output.flavor_primary = await order_items.getFlavor_primary({
transaction
});
output.flavor_secondary = await order_items.getFlavor_secondary({
transaction
});
output.beverage = await order_items.getBeverage({
transaction
});
output.add_on = await order_items.getAdd_on({
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.orders,
as: 'order',
where: filter.order ? {
[Op.or]: [
{ id: { [Op.in]: filter.order.split('|').map(term => Utils.uuid(term)) } },
{
order_number: {
[Op.or]: filter.order.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.pizza_sizes,
as: 'pizza_size',
where: filter.pizza_size ? {
[Op.or]: [
{ id: { [Op.in]: filter.pizza_size.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.pizza_size.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.stuffed_crusts,
as: 'stuffed_crust',
where: filter.stuffed_crust ? {
[Op.or]: [
{ id: { [Op.in]: filter.stuffed_crust.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.stuffed_crust.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.pizza_flavors,
as: 'flavor_primary',
where: filter.flavor_primary ? {
[Op.or]: [
{ id: { [Op.in]: filter.flavor_primary.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.flavor_primary.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.pizza_flavors,
as: 'flavor_secondary',
where: filter.flavor_secondary ? {
[Op.or]: [
{ id: { [Op.in]: filter.flavor_secondary.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.flavor_secondary.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.beverages,
as: 'beverage',
where: filter.beverage ? {
[Op.or]: [
{ id: { [Op.in]: filter.beverage.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.beverage.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.add_ons,
as: 'add_on',
where: filter.add_on ? {
[Op.or]: [
{ id: { [Op.in]: filter.add_on.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.add_on.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.item_note) {
where = {
...where,
[Op.and]: Utils.ilike(
'order_items',
'item_note',
filter.item_note,
),
};
}
if (filter.quantityRange) {
const [start, end] = filter.quantityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
quantity: {
...where.quantity,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
quantity: {
...where.quantity,
[Op.lte]: end,
},
};
}
}
if (filter.unit_priceRange) {
const [start, end] = filter.unit_priceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
unit_price: {
...where.unit_price,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
unit_price: {
...where.unit_price,
[Op.lte]: end,
},
};
}
}
if (filter.total_priceRange) {
const [start, end] = filter.total_priceRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
total_price: {
...where.total_price,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
total_price: {
...where.total_price,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.item_type) {
where = {
...where,
item_type: filter.item_type,
};
}
if (filter.pizza_category) {
where = {
...where,
pizza_category: filter.pizza_category,
};
}
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.order_items.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(
'order_items',
'item_type',
query,
),
],
};
}
const records = await db.order_items.findAll({
attributes: [ 'id', 'item_type' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['item_type', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.item_type,
}));
}
};