391 lines
9.7 KiB
JavaScript
391 lines
9.7 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 Deposit_historiesDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deposit_histories = await db.deposit_histories.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
deposit_amount: data.deposit_amount || null,
|
|
buyer_name: data.buyer_name || null,
|
|
deposit_date: data.deposit_date || null,
|
|
staff_name: data.staff_name || null,
|
|
notes: data.notes || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await deposit_histories.setInventory_item(data.inventory_item || null, {
|
|
transaction,
|
|
});
|
|
|
|
return deposit_histories;
|
|
}
|
|
|
|
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 deposit_historiesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
deposit_amount: item.deposit_amount || null,
|
|
buyer_name: item.buyer_name || null,
|
|
deposit_date: item.deposit_date || null,
|
|
staff_name: item.staff_name || null,
|
|
notes: item.notes || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const deposit_histories = await db.deposit_histories.bulkCreate(
|
|
deposit_historiesData,
|
|
{ transaction },
|
|
);
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return deposit_histories;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deposit_histories = await db.deposit_histories.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.deposit_amount !== undefined)
|
|
updatePayload.deposit_amount = data.deposit_amount;
|
|
|
|
if (data.buyer_name !== undefined)
|
|
updatePayload.buyer_name = data.buyer_name;
|
|
|
|
if (data.deposit_date !== undefined)
|
|
updatePayload.deposit_date = data.deposit_date;
|
|
|
|
if (data.staff_name !== undefined)
|
|
updatePayload.staff_name = data.staff_name;
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await deposit_histories.update(updatePayload, { transaction });
|
|
|
|
if (data.inventory_item !== undefined) {
|
|
await deposit_histories.setInventory_item(
|
|
data.inventory_item,
|
|
|
|
{ transaction },
|
|
);
|
|
}
|
|
|
|
return deposit_histories;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deposit_histories = await db.deposit_histories.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of deposit_histories) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of deposit_histories) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return deposit_histories;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deposit_histories = await db.deposit_histories.findByPk(id, options);
|
|
|
|
await deposit_histories.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await deposit_histories.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return deposit_histories;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const deposit_histories = await db.deposit_histories.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!deposit_histories) {
|
|
return deposit_histories;
|
|
}
|
|
|
|
const output = deposit_histories.get({ plain: true });
|
|
|
|
output.inventory_item = await deposit_histories.getInventory_item({
|
|
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.inventory_items,
|
|
as: 'inventory_item',
|
|
|
|
where: filter.inventory_item
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: filter.inventory_item
|
|
.split('|')
|
|
.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
sku: {
|
|
[Op.or]: filter.inventory_item
|
|
.split('|')
|
|
.map((term) => ({ [Op.iLike]: `%${term}%` })),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {},
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.buyer_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'deposit_histories',
|
|
'buyer_name',
|
|
filter.buyer_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.staff_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'deposit_histories',
|
|
'staff_name',
|
|
filter.staff_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('deposit_histories', 'notes', filter.notes),
|
|
};
|
|
}
|
|
|
|
if (filter.deposit_amountRange) {
|
|
const [start, end] = filter.deposit_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
deposit_amount: {
|
|
...where.deposit_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
deposit_amount: {
|
|
...where.deposit_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.deposit_dateRange) {
|
|
const [start, end] = filter.deposit_dateRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
deposit_date: {
|
|
...where.deposit_date,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
deposit_date: {
|
|
...where.deposit_date,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
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.deposit_histories.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('deposit_histories', 'buyer_name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.deposit_histories.findAll({
|
|
attributes: ['id', 'buyer_name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['buyer_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.buyer_name,
|
|
}));
|
|
}
|
|
};
|