696 lines
19 KiB
JavaScript
696 lines
19 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 Payment_rulesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payment_rules = await db.payment_rules.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
auto_savings_transfers_enabled: data.auto_savings_transfers_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
max_auto_transfer_amount: data.max_auto_transfer_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
credit_card_cashback_enabled: data.credit_card_cashback_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
require_statement_paid_in_full: data.require_statement_paid_in_full
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
fx_optimisation_enabled: data.fx_optimisation_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
international_transfer_policy: data.international_transfer_policy
|
|
||
|
|
null
|
|
,
|
|
|
|
international_auto_approve_limit: data.international_auto_approve_limit
|
|
||
|
|
null
|
|
,
|
|
|
|
selected_spending_categories: data.selected_spending_categories
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_from: data.effective_from
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_to: data.effective_to
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await payment_rules.setUser( data.user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await payment_rules.setPrimary_account( data.primary_account || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return payment_rules;
|
|
}
|
|
|
|
|
|
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 payment_rulesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
auto_savings_transfers_enabled: item.auto_savings_transfers_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
max_auto_transfer_amount: item.max_auto_transfer_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
credit_card_cashback_enabled: item.credit_card_cashback_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
require_statement_paid_in_full: item.require_statement_paid_in_full
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
fx_optimisation_enabled: item.fx_optimisation_enabled
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
international_transfer_policy: item.international_transfer_policy
|
|
||
|
|
null
|
|
,
|
|
|
|
international_auto_approve_limit: item.international_auto_approve_limit
|
|
||
|
|
null
|
|
,
|
|
|
|
selected_spending_categories: item.selected_spending_categories
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_from: item.effective_from
|
|
||
|
|
null
|
|
,
|
|
|
|
effective_to: item.effective_to
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const payment_rules = await db.payment_rules.bulkCreate(payment_rulesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return payment_rules;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const payment_rules = await db.payment_rules.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.auto_savings_transfers_enabled !== undefined) updatePayload.auto_savings_transfers_enabled = data.auto_savings_transfers_enabled;
|
|
|
|
|
|
if (data.max_auto_transfer_amount !== undefined) updatePayload.max_auto_transfer_amount = data.max_auto_transfer_amount;
|
|
|
|
|
|
if (data.credit_card_cashback_enabled !== undefined) updatePayload.credit_card_cashback_enabled = data.credit_card_cashback_enabled;
|
|
|
|
|
|
if (data.require_statement_paid_in_full !== undefined) updatePayload.require_statement_paid_in_full = data.require_statement_paid_in_full;
|
|
|
|
|
|
if (data.fx_optimisation_enabled !== undefined) updatePayload.fx_optimisation_enabled = data.fx_optimisation_enabled;
|
|
|
|
|
|
if (data.international_transfer_policy !== undefined) updatePayload.international_transfer_policy = data.international_transfer_policy;
|
|
|
|
|
|
if (data.international_auto_approve_limit !== undefined) updatePayload.international_auto_approve_limit = data.international_auto_approve_limit;
|
|
|
|
|
|
if (data.selected_spending_categories !== undefined) updatePayload.selected_spending_categories = data.selected_spending_categories;
|
|
|
|
|
|
if (data.effective_from !== undefined) updatePayload.effective_from = data.effective_from;
|
|
|
|
|
|
if (data.effective_to !== undefined) updatePayload.effective_to = data.effective_to;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await payment_rules.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.user !== undefined) {
|
|
await payment_rules.setUser(
|
|
|
|
data.user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.primary_account !== undefined) {
|
|
await payment_rules.setPrimary_account(
|
|
|
|
data.primary_account,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return payment_rules;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payment_rules = await db.payment_rules.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of payment_rules) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of payment_rules) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return payment_rules;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payment_rules = await db.payment_rules.findByPk(id, options);
|
|
|
|
await payment_rules.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await payment_rules.destroy({
|
|
transaction
|
|
});
|
|
|
|
return payment_rules;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const payment_rules = await db.payment_rules.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!payment_rules) {
|
|
return payment_rules;
|
|
}
|
|
|
|
const output = payment_rules.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.user = await payment_rules.getUser({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.primary_account = await payment_rules.getPrimary_account({
|
|
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.accounts,
|
|
as: 'primary_account',
|
|
|
|
where: filter.primary_account ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.primary_account.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
account_name: {
|
|
[Op.or]: filter.primary_account.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.selected_spending_categories) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'payment_rules',
|
|
'selected_spending_categories',
|
|
filter.selected_spending_categories,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
if (filter.calendarStart && filter.calendarEnd) {
|
|
where = {
|
|
...where,
|
|
[Op.or]: [
|
|
{
|
|
effective_from: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
{
|
|
effective_to: {
|
|
[Op.between]: [filter.calendarStart, filter.calendarEnd],
|
|
},
|
|
},
|
|
],
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.max_auto_transfer_amountRange) {
|
|
const [start, end] = filter.max_auto_transfer_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
max_auto_transfer_amount: {
|
|
...where.max_auto_transfer_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
max_auto_transfer_amount: {
|
|
...where.max_auto_transfer_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.international_auto_approve_limitRange) {
|
|
const [start, end] = filter.international_auto_approve_limitRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
international_auto_approve_limit: {
|
|
...where.international_auto_approve_limit,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
international_auto_approve_limit: {
|
|
...where.international_auto_approve_limit,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.effective_fromRange) {
|
|
const [start, end] = filter.effective_fromRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
effective_from: {
|
|
...where.effective_from,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
effective_from: {
|
|
...where.effective_from,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.effective_toRange) {
|
|
const [start, end] = filter.effective_toRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
effective_to: {
|
|
...where.effective_to,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
effective_to: {
|
|
...where.effective_to,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.auto_savings_transfers_enabled) {
|
|
where = {
|
|
...where,
|
|
auto_savings_transfers_enabled: filter.auto_savings_transfers_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.credit_card_cashback_enabled) {
|
|
where = {
|
|
...where,
|
|
credit_card_cashback_enabled: filter.credit_card_cashback_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.require_statement_paid_in_full) {
|
|
where = {
|
|
...where,
|
|
require_statement_paid_in_full: filter.require_statement_paid_in_full,
|
|
};
|
|
}
|
|
|
|
if (filter.fx_optimisation_enabled) {
|
|
where = {
|
|
...where,
|
|
fx_optimisation_enabled: filter.fx_optimisation_enabled,
|
|
};
|
|
}
|
|
|
|
if (filter.international_transfer_policy) {
|
|
where = {
|
|
...where,
|
|
international_transfer_policy: filter.international_transfer_policy,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.payment_rules.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(
|
|
'payment_rules',
|
|
'selected_spending_categories',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.payment_rules.findAll({
|
|
attributes: [ 'id', 'selected_spending_categories' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['selected_spending_categories', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.selected_spending_categories,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|