425 lines
10 KiB
JavaScript
425 lines
10 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 MerchantsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const merchants = await db.merchants.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
merchant_name: data.merchant_name
|
|
||
|
|
null
|
|
,
|
|
|
|
merchant_country: data.merchant_country
|
|
||
|
|
null
|
|
,
|
|
|
|
merchant_category: data.merchant_category
|
|
||
|
|
null
|
|
,
|
|
|
|
icon_hint: data.icon_hint
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return merchants;
|
|
}
|
|
|
|
|
|
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 merchantsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
merchant_name: item.merchant_name
|
|
||
|
|
null
|
|
,
|
|
|
|
merchant_country: item.merchant_country
|
|
||
|
|
null
|
|
,
|
|
|
|
merchant_category: item.merchant_category
|
|
||
|
|
null
|
|
,
|
|
|
|
icon_hint: item.icon_hint
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const merchants = await db.merchants.bulkCreate(merchantsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return merchants;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const merchants = await db.merchants.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.merchant_name !== undefined) updatePayload.merchant_name = data.merchant_name;
|
|
|
|
|
|
if (data.merchant_country !== undefined) updatePayload.merchant_country = data.merchant_country;
|
|
|
|
|
|
if (data.merchant_category !== undefined) updatePayload.merchant_category = data.merchant_category;
|
|
|
|
|
|
if (data.icon_hint !== undefined) updatePayload.icon_hint = data.icon_hint;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await merchants.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return merchants;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const merchants = await db.merchants.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of merchants) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of merchants) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return merchants;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const merchants = await db.merchants.findByPk(id, options);
|
|
|
|
await merchants.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await merchants.destroy({
|
|
transaction
|
|
});
|
|
|
|
return merchants;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const merchants = await db.merchants.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!merchants) {
|
|
return merchants;
|
|
}
|
|
|
|
const output = merchants.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.card_authorisations_merchant = await merchants.getCard_authorisations_merchant({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.transactions_merchant = await merchants.getTransactions_merchant({
|
|
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.merchant_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'merchants',
|
|
'merchant_name',
|
|
filter.merchant_name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.merchant_country) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'merchants',
|
|
'merchant_country',
|
|
filter.merchant_country,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.icon_hint) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'merchants',
|
|
'icon_hint',
|
|
filter.icon_hint,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.merchant_category) {
|
|
where = {
|
|
...where,
|
|
merchant_category: filter.merchant_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.merchants.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(
|
|
'merchants',
|
|
'merchant_name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.merchants.findAll({
|
|
attributes: [ 'id', 'merchant_name' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['merchant_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.merchant_name,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|