39356-vm/backend/src/db/api/voucher_redemptions.js
2026-03-28 03:29:25 +00:00

675 lines
17 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 Voucher_redemptionsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const voucher_redemptions = await db.voucher_redemptions.create(
{
id: data.id || undefined,
points_spent: data.points_spent
||
null
,
redeemed_at: data.redeemed_at
||
null
,
status: data.status
||
null
,
voucher_code: data.voucher_code
||
null
,
delivery_destination: data.delivery_destination
||
null
,
delivered_at: data.delivered_at
||
null
,
expires_at: data.expires_at
||
null
,
external_fulfillment_ref: data.external_fulfillment_ref
||
null
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await voucher_redemptions.setUser( data.user || null, {
transaction,
});
await voucher_redemptions.setVoucher_offer( data.voucher_offer || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.voucher_redemptions.getTableName(),
belongsToColumn: 'voucher_file',
belongsToId: voucher_redemptions.id,
},
data.voucher_file,
options,
);
return voucher_redemptions;
}
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 voucher_redemptionsData = data.map((item, index) => ({
id: item.id || undefined,
points_spent: item.points_spent
||
null
,
redeemed_at: item.redeemed_at
||
null
,
status: item.status
||
null
,
voucher_code: item.voucher_code
||
null
,
delivery_destination: item.delivery_destination
||
null
,
delivered_at: item.delivered_at
||
null
,
expires_at: item.expires_at
||
null
,
external_fulfillment_ref: item.external_fulfillment_ref
||
null
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const voucher_redemptions = await db.voucher_redemptions.bulkCreate(voucher_redemptionsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < voucher_redemptions.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.voucher_redemptions.getTableName(),
belongsToColumn: 'voucher_file',
belongsToId: voucher_redemptions[i].id,
},
data[i].voucher_file,
options,
);
}
return voucher_redemptions;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const voucher_redemptions = await db.voucher_redemptions.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.points_spent !== undefined) updatePayload.points_spent = data.points_spent;
if (data.redeemed_at !== undefined) updatePayload.redeemed_at = data.redeemed_at;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.voucher_code !== undefined) updatePayload.voucher_code = data.voucher_code;
if (data.delivery_destination !== undefined) updatePayload.delivery_destination = data.delivery_destination;
if (data.delivered_at !== undefined) updatePayload.delivered_at = data.delivered_at;
if (data.expires_at !== undefined) updatePayload.expires_at = data.expires_at;
if (data.external_fulfillment_ref !== undefined) updatePayload.external_fulfillment_ref = data.external_fulfillment_ref;
updatePayload.updatedById = currentUser.id;
await voucher_redemptions.update(updatePayload, {transaction});
if (data.user !== undefined) {
await voucher_redemptions.setUser(
data.user,
{ transaction }
);
}
if (data.voucher_offer !== undefined) {
await voucher_redemptions.setVoucher_offer(
data.voucher_offer,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.voucher_redemptions.getTableName(),
belongsToColumn: 'voucher_file',
belongsToId: voucher_redemptions.id,
},
data.voucher_file,
options,
);
return voucher_redemptions;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const voucher_redemptions = await db.voucher_redemptions.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of voucher_redemptions) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of voucher_redemptions) {
await record.destroy({transaction});
}
});
return voucher_redemptions;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const voucher_redemptions = await db.voucher_redemptions.findByPk(id, options);
await voucher_redemptions.update({
deletedBy: currentUser.id
}, {
transaction,
});
await voucher_redemptions.destroy({
transaction
});
return voucher_redemptions;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const voucher_redemptions = await db.voucher_redemptions.findOne(
{ where },
{ transaction },
);
if (!voucher_redemptions) {
return voucher_redemptions;
}
const output = voucher_redemptions.get({plain: true});
output.user = await voucher_redemptions.getUser({
transaction
});
output.voucher_offer = await voucher_redemptions.getVoucher_offer({
transaction
});
output.voucher_file = await voucher_redemptions.getVoucher_file({
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: 'user',
where: filter.user ? {
[Op.or]: [
{ id: { [Op.in]: filter.user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.voucher_offers,
as: 'voucher_offer',
where: filter.voucher_offer ? {
[Op.or]: [
{ id: { [Op.in]: filter.voucher_offer.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.voucher_offer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'voucher_file',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.voucher_code) {
where = {
...where,
[Op.and]: Utils.ilike(
'voucher_redemptions',
'voucher_code',
filter.voucher_code,
),
};
}
if (filter.delivery_destination) {
where = {
...where,
[Op.and]: Utils.ilike(
'voucher_redemptions',
'delivery_destination',
filter.delivery_destination,
),
};
}
if (filter.external_fulfillment_ref) {
where = {
...where,
[Op.and]: Utils.ilike(
'voucher_redemptions',
'external_fulfillment_ref',
filter.external_fulfillment_ref,
),
};
}
if (filter.points_spentRange) {
const [start, end] = filter.points_spentRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
points_spent: {
...where.points_spent,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
points_spent: {
...where.points_spent,
[Op.lte]: end,
},
};
}
}
if (filter.redeemed_atRange) {
const [start, end] = filter.redeemed_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
redeemed_at: {
...where.redeemed_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
redeemed_at: {
...where.redeemed_at,
[Op.lte]: end,
},
};
}
}
if (filter.delivered_atRange) {
const [start, end] = filter.delivered_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
delivered_at: {
...where.delivered_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
delivered_at: {
...where.delivered_at,
[Op.lte]: end,
},
};
}
}
if (filter.expires_atRange) {
const [start, end] = filter.expires_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
expires_at: {
...where.expires_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
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.voucher_redemptions.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(
'voucher_redemptions',
'voucher_code',
query,
),
],
};
}
const records = await db.voucher_redemptions.findAll({
attributes: [ 'id', 'voucher_code' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['voucher_code', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.voucher_code,
}));
}
};