568 lines
14 KiB
JavaScript
568 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 Crew_membersDBApi {
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const crew_members = await db.crew_members.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
full_name: data.full_name || null,
|
|
designation: data.designation || null,
|
|
aircraft_type: data.aircraft_type || null,
|
|
base: data.base || null,
|
|
marital_status: data.marital_status || null,
|
|
department: data.department || null,
|
|
contact_details: data.contact_details || null,
|
|
crew_id: data.crew_id || null,
|
|
education: data.education || null,
|
|
nationality: data.nationality || null,
|
|
gender: data.gender || null,
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
await crew_members.setDocuments(data.documents || [], {
|
|
transaction,
|
|
});
|
|
|
|
await crew_members.setLicenses(data.licenses || [], {
|
|
transaction,
|
|
});
|
|
|
|
await crew_members.setPerformance_metrics(data.performance_metrics || [], {
|
|
transaction,
|
|
});
|
|
|
|
return crew_members;
|
|
}
|
|
|
|
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 crew_membersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
full_name: item.full_name || null,
|
|
designation: item.designation || null,
|
|
aircraft_type: item.aircraft_type || null,
|
|
base: item.base || null,
|
|
marital_status: item.marital_status || null,
|
|
department: item.department || null,
|
|
contact_details: item.contact_details || null,
|
|
crew_id: item.crew_id || null,
|
|
education: item.education || null,
|
|
nationality: item.nationality || null,
|
|
gender: item.gender || null,
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const crew_members = await db.crew_members.bulkCreate(crew_membersData, {
|
|
transaction,
|
|
});
|
|
|
|
// For each item created, replace relation files
|
|
|
|
return crew_members;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const crew_members = await db.crew_members.findByPk(
|
|
id,
|
|
{},
|
|
{ transaction },
|
|
);
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.full_name !== undefined) updatePayload.full_name = data.full_name;
|
|
|
|
if (data.designation !== undefined)
|
|
updatePayload.designation = data.designation;
|
|
|
|
if (data.aircraft_type !== undefined)
|
|
updatePayload.aircraft_type = data.aircraft_type;
|
|
|
|
if (data.base !== undefined) updatePayload.base = data.base;
|
|
|
|
if (data.marital_status !== undefined)
|
|
updatePayload.marital_status = data.marital_status;
|
|
|
|
if (data.department !== undefined)
|
|
updatePayload.department = data.department;
|
|
|
|
if (data.contact_details !== undefined)
|
|
updatePayload.contact_details = data.contact_details;
|
|
|
|
if (data.crew_id !== undefined) updatePayload.crew_id = data.crew_id;
|
|
|
|
if (data.education !== undefined) updatePayload.education = data.education;
|
|
|
|
if (data.nationality !== undefined)
|
|
updatePayload.nationality = data.nationality;
|
|
|
|
if (data.gender !== undefined) updatePayload.gender = data.gender;
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await crew_members.update(updatePayload, { transaction });
|
|
|
|
if (data.documents !== undefined) {
|
|
await crew_members.setDocuments(data.documents, { transaction });
|
|
}
|
|
|
|
if (data.licenses !== undefined) {
|
|
await crew_members.setLicenses(data.licenses, { transaction });
|
|
}
|
|
|
|
if (data.performance_metrics !== undefined) {
|
|
await crew_members.setPerformance_metrics(data.performance_metrics, {
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
return crew_members;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const crew_members = await db.crew_members.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of crew_members) {
|
|
await record.update({ deletedBy: currentUser.id }, { transaction });
|
|
}
|
|
for (const record of crew_members) {
|
|
await record.destroy({ transaction });
|
|
}
|
|
});
|
|
|
|
return crew_members;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const crew_members = await db.crew_members.findByPk(id, options);
|
|
|
|
await crew_members.update(
|
|
{
|
|
deletedBy: currentUser.id,
|
|
},
|
|
{
|
|
transaction,
|
|
},
|
|
);
|
|
|
|
await crew_members.destroy({
|
|
transaction,
|
|
});
|
|
|
|
return crew_members;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const crew_members = await db.crew_members.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!crew_members) {
|
|
return crew_members;
|
|
}
|
|
|
|
const output = crew_members.get({ plain: true });
|
|
|
|
output.documents_crew_member = await crew_members.getDocuments_crew_member({
|
|
transaction,
|
|
});
|
|
|
|
output.licenses_crew_member = await crew_members.getLicenses_crew_member({
|
|
transaction,
|
|
});
|
|
|
|
output.performance_metrics_crew_member =
|
|
await crew_members.getPerformance_metrics_crew_member({
|
|
transaction,
|
|
});
|
|
|
|
output.uniform_requests_crew_member =
|
|
await crew_members.getUniform_requests_crew_member({
|
|
transaction,
|
|
});
|
|
|
|
output.documents = await crew_members.getDocuments({
|
|
transaction,
|
|
});
|
|
|
|
output.licenses = await crew_members.getLicenses({
|
|
transaction,
|
|
});
|
|
|
|
output.performance_metrics = await crew_members.getPerformance_metrics({
|
|
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.documents,
|
|
as: 'documents',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.licenses,
|
|
as: 'licenses',
|
|
required: false,
|
|
},
|
|
|
|
{
|
|
model: db.performance_metrics,
|
|
as: 'performance_metrics',
|
|
required: false,
|
|
},
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
if (filter.full_name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('crew_members', 'full_name', filter.full_name),
|
|
};
|
|
}
|
|
|
|
if (filter.designation) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'crew_members',
|
|
'designation',
|
|
filter.designation,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.aircraft_type) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'crew_members',
|
|
'aircraft_type',
|
|
filter.aircraft_type,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.base) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('crew_members', 'base', filter.base),
|
|
};
|
|
}
|
|
|
|
if (filter.marital_status) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'crew_members',
|
|
'marital_status',
|
|
filter.marital_status,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.department) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'crew_members',
|
|
'department',
|
|
filter.department,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.contact_details) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'crew_members',
|
|
'contact_details',
|
|
filter.contact_details,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.crew_id) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('crew_members', 'crew_id', filter.crew_id),
|
|
};
|
|
}
|
|
|
|
if (filter.education) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('crew_members', 'education', filter.education),
|
|
};
|
|
}
|
|
|
|
if (filter.nationality) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'crew_members',
|
|
'nationality',
|
|
filter.nationality,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.gender) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike('crew_members', 'gender', filter.gender),
|
|
};
|
|
}
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true',
|
|
};
|
|
}
|
|
|
|
if (filter.documents) {
|
|
const searchTerms = filter.documents.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.documents,
|
|
as: 'documents_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
document_type: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.licenses) {
|
|
const searchTerms = filter.licenses.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.licenses,
|
|
as: 'licenses_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
license_type: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
if (filter.performance_metrics) {
|
|
const searchTerms = filter.performance_metrics.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.performance_metrics,
|
|
as: 'performance_metrics_filter',
|
|
required: searchTerms.length > 0,
|
|
where:
|
|
searchTerms.length > 0
|
|
? {
|
|
[Op.or]: [
|
|
{
|
|
id: {
|
|
[Op.in]: searchTerms.map((term) => Utils.uuid(term)),
|
|
},
|
|
},
|
|
{
|
|
flight_hours: {
|
|
[Op.or]: searchTerms.map((term) => ({
|
|
[Op.iLike]: `%${term}%`,
|
|
})),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: undefined,
|
|
},
|
|
...include,
|
|
];
|
|
}
|
|
|
|
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.crew_members.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('crew_members', 'full_name', query),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.crew_members.findAll({
|
|
attributes: ['id', 'full_name'],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['full_name', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.full_name,
|
|
}));
|
|
}
|
|
};
|