fix: transaccionado por trabajador y día
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2023-01-18 14:23:05 +01:00
parent 7368b572a0
commit 2c995208d1
1 changed files with 78 additions and 77 deletions

View File

@ -38,87 +38,81 @@ module.exports = Self => {
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
const stmts = [];
let stmt;
try {
if (!args.week || !args.year) {
const from = new Date();
const to = new Date();
if (!args.week || !args.year) {
const from = new Date();
const to = new Date();
const time = await models.Time.findOne({
where: {
dated: {between: [from.setDate(from.getDate() - 10), to.setDate(to.getDate() - 4)]}
},
order: 'week ASC'
}, myOptions);
const time = await models.Time.findOne({
where: {
dated: {between: [from.setDate(from.getDate() - 10), to.setDate(to.getDate() - 4)]}
},
order: 'week ASC'
}, myOptions);
args.week = time.week;
args.year = time.year;
}
args.week = time.week;
args.year = time.year;
}
const started = getStartDateOfWeekNumber(args.week, args.year);
started.setHours(0, 0, 0, 0);
const started = getStartDateOfWeekNumber(args.week, args.year);
started.setHours(0, 0, 0, 0);
const ended = new Date(started);
ended.setDate(started.getDate() + 6);
ended.setHours(23, 59, 59, 999);
const ended = new Date(started);
ended.setDate(started.getDate() + 6);
ended.setHours(23, 59, 59, 999);
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.timeControlCalculate');
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.timeBusinessCalculate');
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.timeControlCalculate');
stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.timeBusinessCalculate');
if (args.workerId) {
await models.WorkerTimeControl.destroyAll({
userFk: args.workerId,
timed: {between: [started, ended]},
isSendMail: true
}, myOptions);
if (args.workerId) {
await models.WorkerTimeControl.destroyAll({
userFk: args.workerId,
timed: {between: [started, ended]},
isSendMail: true
}, myOptions);
const where = {
workerFk: args.workerId,
year: args.year,
week: args.week
};
await models.WorkerTimeControlMail.updateAll(where, {
updated: new Date(), state: 'SENDED'
}, myOptions);
const where = {
workerFk: args.workerId,
year: args.year,
week: args.week
};
await models.WorkerTimeControlMail.updateAll(where, {
updated: new Date(), state: 'SENDED'
}, myOptions);
stmt = new ParameterizedSQL(
`CALL vn.timeControl_calculateByUser(?, ?, ?)
stmt = new ParameterizedSQL(
`CALL vn.timeControl_calculateByUser(?, ?, ?)
`, [args.workerId, started, ended]);
stmts.push(stmt);
stmts.push(stmt);
stmt = new ParameterizedSQL(
`CALL vn.timeBusiness_calculateByUser(?, ?, ?)
stmt = new ParameterizedSQL(
`CALL vn.timeBusiness_calculateByUser(?, ?, ?)
`, [args.workerId, started, ended]);
stmts.push(stmt);
} else {
await models.WorkerTimeControl.destroyAll({
timed: {between: [started, ended]},
isSendMail: true
}, myOptions);
stmts.push(stmt);
} else {
await models.WorkerTimeControl.destroyAll({
timed: {between: [started, ended]},
isSendMail: true
}, myOptions);
const where = {
year: args.year,
week: args.week
};
await models.WorkerTimeControlMail.updateAll(where, {
updated: new Date(), state: 'SENDED'
}, myOptions);
const where = {
year: args.year,
week: args.week
};
await models.WorkerTimeControlMail.updateAll(where, {
updated: new Date(), state: 'SENDED'
}, myOptions);
stmt = new ParameterizedSQL(`CALL vn.timeControl_calculateAll(?, ?)`, [started, ended]);
stmts.push(stmt);
stmt = new ParameterizedSQL(`CALL vn.timeControl_calculateAll(?, ?)`, [started, ended]);
stmts.push(stmt);
stmt = new ParameterizedSQL(`CALL vn.timeBusiness_calculateAll(?, ?)`, [started, ended]);
stmts.push(stmt);
}
stmt = new ParameterizedSQL(`CALL vn.timeBusiness_calculateAll(?, ?)`, [started, ended]);
stmts.push(stmt);
}
stmt = new ParameterizedSQL(`
stmt = new ParameterizedSQL(`
SELECT CONCAT(u.name, '@verdnatura.es') receiver,
u.id workerFk,
tb.dated,
@ -153,18 +147,25 @@ module.exports = Self => {
AND w.businessFk
ORDER BY u.id, tb.dated
`, [args.workerId]);
const index = stmts.push(stmt) - 1;
const index = stmts.push(stmt) - 1;
const sql = ParameterizedSQL.join(stmts, ';');
const days = await conn.executeStmt(sql, myOptions);
stmts.push('DROP TEMPORARY TABLE tmp.timeControlCalculate');
stmts.push('DROP TEMPORARY TABLE tmp.timeBusinessCalculate');
let previousWorkerFk = days[index][0].workerFk;
let previousReceiver = days[index][0].receiver;
const sql = ParameterizedSQL.join(stmts, ';');
const days = await conn.executeStmt(sql, myOptions);
const workerTimeControlConfig = await models.WorkerTimeControlConfig.findOne(null, myOptions);
let previousWorkerFk = days[index][0].workerFk;
let previousReceiver = days[index][0].receiver;
for (let day of days[index]) {
const workerTimeControlConfig = await models.WorkerTimeControlConfig.findOne(null, myOptions);
for (let day of days[index]) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
try {
workerFk = day.workerFk;
if (workerFk == 418) throw new Error('Test');
if (day.timeWorkDecimal > 0 && day.timeWorkedDecimal == null
&& (day.permissionRate == null ? true : day.permissionRate)) {
if (day.timeTable == null) {
@ -345,15 +346,15 @@ module.exports = Self => {
previousWorkerFk = day.workerFk;
previousReceiver = day.receiver;
}
if (tx) await tx.commit();
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
if (tx) await tx.commit();
return true;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
return true;
};
function getStartDateOfWeekNumber(week, year) {