827 lines
21 KiB
JavaScript
827 lines
21 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 Freelancer_paymentsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const freelancer_payments = await db.freelancer_payments.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
amount: data.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency_code: data.currency_code
|
|
||
|
|
null
|
|
,
|
|
|
|
paid_at: data.paid_at
|
|
||
|
|
null
|
|
,
|
|
|
|
method: data.method
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
reference: data.reference
|
|
||
|
|
null
|
|
,
|
|
|
|
hours_billed: data.hours_billed
|
|
||
|
|
null
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await freelancer_payments.setTenant( data.tenant || null, {
|
|
transaction,
|
|
});
|
|
|
|
await freelancer_payments.setFreelancer( data.freelancer || null, {
|
|
transaction,
|
|
});
|
|
|
|
await freelancer_payments.setFreelancer_assignment( data.freelancer_assignment || null, {
|
|
transaction,
|
|
});
|
|
|
|
await freelancer_payments.setProject( data.project || null, {
|
|
transaction,
|
|
});
|
|
|
|
await freelancer_payments.setRecorded_by( data.recorded_by || null, {
|
|
transaction,
|
|
});
|
|
|
|
await freelancer_payments.setOrganizations( data.organizations || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.freelancer_payments.getTableName(),
|
|
belongsToColumn: 'receipt_files',
|
|
belongsToId: freelancer_payments.id,
|
|
},
|
|
data.receipt_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return freelancer_payments;
|
|
}
|
|
|
|
|
|
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 freelancer_paymentsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
amount: item.amount
|
|
||
|
|
null
|
|
,
|
|
|
|
currency_code: item.currency_code
|
|
||
|
|
null
|
|
,
|
|
|
|
paid_at: item.paid_at
|
|
||
|
|
null
|
|
,
|
|
|
|
method: item.method
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
reference: item.reference
|
|
||
|
|
null
|
|
,
|
|
|
|
hours_billed: item.hours_billed
|
|
||
|
|
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 freelancer_payments = await db.freelancer_payments.bulkCreate(freelancer_paymentsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < freelancer_payments.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.freelancer_payments.getTableName(),
|
|
belongsToColumn: 'receipt_files',
|
|
belongsToId: freelancer_payments[i].id,
|
|
},
|
|
data[i].receipt_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return freelancer_payments;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const freelancer_payments = await db.freelancer_payments.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.amount !== undefined) updatePayload.amount = data.amount;
|
|
|
|
|
|
if (data.currency_code !== undefined) updatePayload.currency_code = data.currency_code;
|
|
|
|
|
|
if (data.paid_at !== undefined) updatePayload.paid_at = data.paid_at;
|
|
|
|
|
|
if (data.method !== undefined) updatePayload.method = data.method;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.reference !== undefined) updatePayload.reference = data.reference;
|
|
|
|
|
|
if (data.hours_billed !== undefined) updatePayload.hours_billed = data.hours_billed;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await freelancer_payments.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.tenant !== undefined) {
|
|
await freelancer_payments.setTenant(
|
|
|
|
data.tenant,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.freelancer !== undefined) {
|
|
await freelancer_payments.setFreelancer(
|
|
|
|
data.freelancer,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.freelancer_assignment !== undefined) {
|
|
await freelancer_payments.setFreelancer_assignment(
|
|
|
|
data.freelancer_assignment,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.project !== undefined) {
|
|
await freelancer_payments.setProject(
|
|
|
|
data.project,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.recorded_by !== undefined) {
|
|
await freelancer_payments.setRecorded_by(
|
|
|
|
data.recorded_by,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.organizations !== undefined) {
|
|
await freelancer_payments.setOrganizations(
|
|
|
|
data.organizations,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.freelancer_payments.getTableName(),
|
|
belongsToColumn: 'receipt_files',
|
|
belongsToId: freelancer_payments.id,
|
|
},
|
|
data.receipt_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return freelancer_payments;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const freelancer_payments = await db.freelancer_payments.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of freelancer_payments) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of freelancer_payments) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return freelancer_payments;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const freelancer_payments = await db.freelancer_payments.findByPk(id, options);
|
|
|
|
await freelancer_payments.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await freelancer_payments.destroy({
|
|
transaction
|
|
});
|
|
|
|
return freelancer_payments;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const freelancer_payments = await db.freelancer_payments.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!freelancer_payments) {
|
|
return freelancer_payments;
|
|
}
|
|
|
|
const output = freelancer_payments.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.tenant = await freelancer_payments.getTenant({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.freelancer = await freelancer_payments.getFreelancer({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.freelancer_assignment = await freelancer_payments.getFreelancer_assignment({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.project = await freelancer_payments.getProject({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.receipt_files = await freelancer_payments.getReceipt_files({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.recorded_by = await freelancer_payments.getRecorded_by({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.organizations = await freelancer_payments.getOrganizations({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userOrganizations = (user && user.organizations?.id) || null;
|
|
|
|
|
|
|
|
if (userOrganizations) {
|
|
if (options?.currentUser?.organizationsId) {
|
|
where.organizationsId = options.currentUser.organizationsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.tenants,
|
|
as: 'tenant',
|
|
|
|
where: filter.tenant ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.tenant.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.tenant.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.freelancers,
|
|
as: 'freelancer',
|
|
|
|
where: filter.freelancer ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.freelancer.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.freelancer.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.freelancer_assignments,
|
|
as: 'freelancer_assignment',
|
|
|
|
where: filter.freelancer_assignment ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.freelancer_assignment.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
role_on_project: {
|
|
[Op.or]: filter.freelancer_assignment.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.projects,
|
|
as: 'project',
|
|
|
|
where: filter.project ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
name: {
|
|
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'recorded_by',
|
|
|
|
where: filter.recorded_by ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.recorded_by.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.recorded_by.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.organizations,
|
|
as: 'organizations',
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'receipt_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.currency_code) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'freelancer_payments',
|
|
'currency_code',
|
|
filter.currency_code,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.reference) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'freelancer_payments',
|
|
'reference',
|
|
filter.reference,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'freelancer_payments',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.amountRange) {
|
|
const [start, end] = filter.amountRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
amount: {
|
|
...where.amount,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
amount: {
|
|
...where.amount,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.paid_atRange) {
|
|
const [start, end] = filter.paid_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
paid_at: {
|
|
...where.paid_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
paid_at: {
|
|
...where.paid_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.hours_billedRange) {
|
|
const [start, end] = filter.hours_billedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
hours_billed: {
|
|
...where.hours_billed,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
hours_billed: {
|
|
...where.hours_billed,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.method) {
|
|
where = {
|
|
...where,
|
|
method: filter.method,
|
|
};
|
|
}
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.organizations) {
|
|
const listItems = filter.organizations.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
organizationsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.organizationsId;
|
|
}
|
|
|
|
|
|
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.freelancer_payments.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, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'freelancer_payments',
|
|
'reference',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.freelancer_payments.findAll({
|
|
attributes: [ 'id', 'reference' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['reference', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.reference,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|