558 lines
14 KiB
JavaScript
558 lines
14 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 Referral_commissionsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const referral_commissions = await db.referral_commissions.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
commission_percent: data.commission_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
commission_amount: data.commission_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
source: data.source
|
|
||
|
|
null
|
|
,
|
|
|
|
source_reference: data.source_reference
|
|
||
|
|
null
|
|
,
|
|
|
|
awarded_at: data.awarded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await referral_commissions.setReferrer_user( data.referrer_user || null, {
|
|
transaction,
|
|
});
|
|
|
|
await referral_commissions.setReferred_user( data.referred_user || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return referral_commissions;
|
|
}
|
|
|
|
|
|
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 referral_commissionsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
commission_percent: item.commission_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
commission_amount: item.commission_amount
|
|
||
|
|
null
|
|
,
|
|
|
|
source: item.source
|
|
||
|
|
null
|
|
,
|
|
|
|
source_reference: item.source_reference
|
|
||
|
|
null
|
|
,
|
|
|
|
awarded_at: item.awarded_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const referral_commissions = await db.referral_commissions.bulkCreate(referral_commissionsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return referral_commissions;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const referral_commissions = await db.referral_commissions.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.commission_percent !== undefined) updatePayload.commission_percent = data.commission_percent;
|
|
|
|
|
|
if (data.commission_amount !== undefined) updatePayload.commission_amount = data.commission_amount;
|
|
|
|
|
|
if (data.source !== undefined) updatePayload.source = data.source;
|
|
|
|
|
|
if (data.source_reference !== undefined) updatePayload.source_reference = data.source_reference;
|
|
|
|
|
|
if (data.awarded_at !== undefined) updatePayload.awarded_at = data.awarded_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await referral_commissions.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.referrer_user !== undefined) {
|
|
await referral_commissions.setReferrer_user(
|
|
|
|
data.referrer_user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.referred_user !== undefined) {
|
|
await referral_commissions.setReferred_user(
|
|
|
|
data.referred_user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return referral_commissions;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const referral_commissions = await db.referral_commissions.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of referral_commissions) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of referral_commissions) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return referral_commissions;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const referral_commissions = await db.referral_commissions.findByPk(id, options);
|
|
|
|
await referral_commissions.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await referral_commissions.destroy({
|
|
transaction
|
|
});
|
|
|
|
return referral_commissions;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const referral_commissions = await db.referral_commissions.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!referral_commissions) {
|
|
return referral_commissions;
|
|
}
|
|
|
|
const output = referral_commissions.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.referrer_user = await referral_commissions.getReferrer_user({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.referred_user = await referral_commissions.getReferred_user({
|
|
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: 'referrer_user',
|
|
|
|
where: filter.referrer_user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.referrer_user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.referrer_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'referred_user',
|
|
|
|
where: filter.referred_user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.referred_user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.referred_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.source) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'referral_commissions',
|
|
'source',
|
|
filter.source,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.source_reference) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'referral_commissions',
|
|
'source_reference',
|
|
filter.source_reference,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.commission_percentRange) {
|
|
const [start, end] = filter.commission_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
commission_percent: {
|
|
...where.commission_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
commission_percent: {
|
|
...where.commission_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.commission_amountRange) {
|
|
const [start, end] = filter.commission_amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
commission_amount: {
|
|
...where.commission_amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
commission_amount: {
|
|
...where.commission_amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.awarded_atRange) {
|
|
const [start, end] = filter.awarded_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
awarded_at: {
|
|
...where.awarded_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
awarded_at: {
|
|
...where.awarded_at,
|
|
[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.referral_commissions.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(
|
|
'referral_commissions',
|
|
'source_reference',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.referral_commissions.findAll({
|
|
attributes: [ 'id', 'source_reference' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['source_reference', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.source_reference,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|