668 lines
16 KiB
JavaScript
668 lines
16 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 OrdersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const orders = await db.orders.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
order_number: data.order_number
|
|
||
|
|
null
|
|
,
|
|
|
|
price: data.price
|
|
||
|
|
null
|
|
,
|
|
|
|
payment_method: data.payment_method
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
contact_info: data.contact_info
|
|
||
|
|
null
|
|
,
|
|
|
|
admin_note: data.admin_note
|
|
||
|
|
null
|
|
,
|
|
|
|
created_date: data.created_date
|
|
||
|
|
null
|
|
,
|
|
|
|
delivered_date: data.delivered_date
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await orders.setBuyer( data.buyer || null, {
|
|
transaction,
|
|
});
|
|
|
|
await orders.setAccount( data.account || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.orders.getTableName(),
|
|
belongsToColumn: 'payment_proof',
|
|
belongsToId: orders.id,
|
|
},
|
|
data.payment_proof,
|
|
options,
|
|
);
|
|
|
|
|
|
return orders;
|
|
}
|
|
|
|
|
|
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 ordersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
order_number: item.order_number
|
|
||
|
|
null
|
|
,
|
|
|
|
price: item.price
|
|
||
|
|
null
|
|
,
|
|
|
|
payment_method: item.payment_method
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
contact_info: item.contact_info
|
|
||
|
|
null
|
|
,
|
|
|
|
admin_note: item.admin_note
|
|
||
|
|
null
|
|
,
|
|
|
|
created_date: item.created_date
|
|
||
|
|
null
|
|
,
|
|
|
|
delivered_date: item.delivered_date
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const orders = await db.orders.bulkCreate(ordersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < orders.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.orders.getTableName(),
|
|
belongsToColumn: 'payment_proof',
|
|
belongsToId: orders[i].id,
|
|
},
|
|
data[i].payment_proof,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return orders;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const orders = await db.orders.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.order_number !== undefined) updatePayload.order_number = data.order_number;
|
|
|
|
|
|
if (data.price !== undefined) updatePayload.price = data.price;
|
|
|
|
|
|
if (data.payment_method !== undefined) updatePayload.payment_method = data.payment_method;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.contact_info !== undefined) updatePayload.contact_info = data.contact_info;
|
|
|
|
|
|
if (data.admin_note !== undefined) updatePayload.admin_note = data.admin_note;
|
|
|
|
|
|
if (data.created_date !== undefined) updatePayload.created_date = data.created_date;
|
|
|
|
|
|
if (data.delivered_date !== undefined) updatePayload.delivered_date = data.delivered_date;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await orders.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.buyer !== undefined) {
|
|
await orders.setBuyer(
|
|
|
|
data.buyer,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.account !== undefined) {
|
|
await orders.setAccount(
|
|
|
|
data.account,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.orders.getTableName(),
|
|
belongsToColumn: 'payment_proof',
|
|
belongsToId: orders.id,
|
|
},
|
|
data.payment_proof,
|
|
options,
|
|
);
|
|
|
|
|
|
return orders;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const orders = await db.orders.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of orders) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of orders) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return orders;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const orders = await db.orders.findByPk(id, options);
|
|
|
|
await orders.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await orders.destroy({
|
|
transaction
|
|
});
|
|
|
|
return orders;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const orders = await db.orders.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!orders) {
|
|
return orders;
|
|
}
|
|
|
|
const output = orders.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.buyer = await orders.getBuyer({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.account = await orders.getAccount({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.payment_proof = await orders.getPayment_proof({
|
|
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: 'buyer',
|
|
|
|
where: filter.buyer ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.buyer.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.buyer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.game_accounts,
|
|
as: 'account',
|
|
|
|
where: filter.account ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.account.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.account.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'payment_proof',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.order_number) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'orders',
|
|
'order_number',
|
|
filter.order_number,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.contact_info) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'orders',
|
|
'contact_info',
|
|
filter.contact_info,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.admin_note) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'orders',
|
|
'admin_note',
|
|
filter.admin_note,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
created_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
delivered_date: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.priceRange) {
|
|
const [start, end] = filter.priceRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
price: {
|
|
...where.price,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
price: {
|
|
...where.price,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.created_dateRange) {
|
|
const [start, end] = filter.created_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
created_date: {
|
|
...where.created_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
created_date: {
|
|
...where.created_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.delivered_dateRange) {
|
|
const [start, end] = filter.delivered_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
delivered_date: {
|
|
...where.delivered_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
delivered_date: {
|
|
...where.delivered_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.payment_method) {
|
|
where = {
|
|
...where,
|
|
payment_method: filter.payment_method,
|
|
};
|
|
}
|
|
|
|
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.orders.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(
|
|
'orders',
|
|
'order_number',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.orders.findAll({
|
|
attributes: [ 'id', 'order_number' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['order_number', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.order_number,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|