2026-03-13 16:17:33 +00:00

153 lines
4.2 KiB
JavaScript

const db = require('../db/models');
const Job_logsDBApi = require('../db/api/job_logs');
const CustomersDBApi = require('../db/api/customers');
const processFile = require("../middlewares/upload");
const ValidationError = require('./notifications/errors/validation');
const csv = require('csv-parser');
const axios = require('axios');
const config = require('../config');
const stream = require('stream');
module.exports = class Job_logsService {
static async create(data, currentUser) {
const transaction = await db.sequelize.transaction();
try {
let customerId = data.customer;
// If customer is a string and not a UUID, try to find or create
if (typeof customerId === 'string' && customerId.length > 0 && !customerId.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)) {
let customer = await db.customers.findOne({ where: { name: customerId }, transaction });
if (!customer) {
customer = await CustomersDBApi.create({ name: customerId }, { currentUser, transaction });
}
customerId = customer.id;
}
await Job_logsDBApi.create(
{ ...data, customer: customerId },
{
currentUser,
transaction,
},
);
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
};
static async bulkImport(req, res, sendInvitationEmails = true, host) {
const transaction = await db.sequelize.transaction();
try {
await processFile(req, res);
const bufferStream = new stream.PassThrough();
const results = [];
await bufferStream.end(Buffer.from(req.file.buffer, "utf-8")); // convert Buffer to Stream
await new Promise((resolve, reject) => {
bufferStream
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', async () => {
console.log('CSV results', results);
resolve();
})
.on('error', (error) => reject(error));
})
await Job_logsDBApi.bulkImport(results, {
transaction,
ignoreDuplicates: true,
validate: true,
currentUser: req.currentUser
});
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
}
static async update(data, id, currentUser) {
const transaction = await db.sequelize.transaction();
try {
let job_logs = await Job_logsDBApi.findBy(
{id},
{transaction},
);
if (!job_logs) {
throw new ValidationError(
'job_logsNotFound',
);
}
let customerId = data.customer;
// If customer is a string and not a UUID, try to find or create
if (typeof customerId === 'string' && customerId.length > 0 && !customerId.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/)) {
let customer = await db.customers.findOne({ where: { name: customerId }, transaction });
if (!customer) {
customer = await CustomersDBApi.create({ name: customerId }, { currentUser, transaction });
}
customerId = customer.id;
}
const updatedJob_logs = await Job_logsDBApi.update(
id,
{ ...data, customer: customerId },
{
currentUser,
transaction,
},
);
await transaction.commit();
return updatedJob_logs;
} catch (error) {
await transaction.rollback();
throw error;
}
};
static async deleteByIds(ids, currentUser) {
const transaction = await db.sequelize.transaction();
try {
await Job_logsDBApi.deleteByIds(ids, {
currentUser,
transaction,
});
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
}
static async remove(id, currentUser) {
const transaction = await db.sequelize.transaction();
try {
await Job_logsDBApi.remove(
id,
{
currentUser,
transaction,
},
);
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
}
};