mirror of
https://github.com/sergeyyarkov/asu_tools_service.git
synced 2026-10-11 10:29:22 +03:00
report executors link
This commit is contained in:
parent
cde03d43a6
commit
ba43273f33
@ -19,7 +19,7 @@ export const reportRepository = {
|
|||||||
const result = await request.query(`select top 1 * from ${ReportTable.tableName} order by id desc`);
|
const result = await request.query(`select top 1 * from ${ReportTable.tableName} order by id desc`);
|
||||||
const report = ReportModel.fromRecordset(result.recordset);
|
const report = ReportModel.fromRecordset(result.recordset);
|
||||||
|
|
||||||
return report[0];
|
return report.at(0);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -50,15 +50,17 @@ export const reportRepository = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Record<string | number, Array<string | number>>} links
|
* @param {Record<string, Array<string | number>>[]} links
|
||||||
* @param {sql.Transaction} [trx]
|
* @param {sql.Transaction} [trx]
|
||||||
*/
|
*/
|
||||||
async attachExecutors(links, trx) {
|
async attachExecutors(links, trx) {
|
||||||
const request = trx ? new sql.Request(trx) : new sql.Request(db);
|
const request = trx ? new sql.Request(trx) : new sql.Request(db);
|
||||||
const table = ReportExecutorTable.createInstance();
|
const table = ReportExecutorTable.createInstance();
|
||||||
|
|
||||||
for (const [rId, executorIds] of Object.entries(links)) {
|
for (const report of Object.values(links)) {
|
||||||
executorIds.forEach((eId) => table.rows.add(rId, eId));
|
for (const [rId, executorIds] of Object.entries(report)) {
|
||||||
|
executorIds.forEach((eId) => table.rows.add(rId, eId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await request.bulk(table);
|
await request.bulk(table);
|
||||||
@ -72,8 +74,8 @@ export const reportRepository = {
|
|||||||
async bulk(entries, trx) {
|
async bulk(entries, trx) {
|
||||||
const request = trx ? new sql.Request(trx) : new sql.Request(db);
|
const request = trx ? new sql.Request(trx) : new sql.Request(db);
|
||||||
const table = ReportTable.createInstance();
|
const table = ReportTable.createInstance();
|
||||||
const lastReportId = (await this.getLast()).id;
|
const lastReportId = (await this.getLast())?.id || 0;
|
||||||
let nextAddId = Number.parseInt(lastReportId, 10);
|
let nextAddId = typeof lastReportId === "string" ? Number.parseInt(lastReportId, 10) : lastReportId;
|
||||||
|
|
||||||
for (const e of entries) {
|
for (const e of entries) {
|
||||||
table.rows.add(
|
table.rows.add(
|
||||||
@ -89,6 +91,6 @@ export const reportRepository = {
|
|||||||
|
|
||||||
const res = await request.bulk(table);
|
const res = await request.bulk(table);
|
||||||
|
|
||||||
return res.rowsAffected;
|
return { rowsAffected: res.rowsAffected, addedRowIds: table.rows.map((r) => r[0]) };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -20,7 +20,7 @@ export const reportItemSchema = y.object({
|
|||||||
job_description: y.string(),
|
job_description: y.string(),
|
||||||
root_cause: y.string(),
|
root_cause: y.string(),
|
||||||
applicant: y.object({ id: y.number(), name: y.string() }).nullable(),
|
applicant: y.object({ id: y.number(), name: y.string() }).nullable(),
|
||||||
executors: y.array(y.object({ id: y.number(), fullname: y.string() })).required()
|
executors: y.array(y.object({ id: y.number().required(), fullname: y.string().required() })).required()
|
||||||
});
|
});
|
||||||
|
|
||||||
export const reportsSchema = y.object({ reports: y.array(reportItemSchema).min(1).required() }).required();
|
export const reportsSchema = y.object({ reports: y.array(reportItemSchema).min(1).required() }).required();
|
||||||
|
|||||||
@ -175,13 +175,10 @@ export const reportsService = {
|
|||||||
* @param {InferType<typeof reportsSchema>['reports']} inReports
|
* @param {InferType<typeof reportsSchema>['reports']} inReports
|
||||||
*/
|
*/
|
||||||
async syncReportsWithDatabase(inReports) {
|
async syncReportsWithDatabase(inReports) {
|
||||||
|
const INVALID_DATE_FALLBACK = new Date("2016-10-24"); // дата для постановки вместо неверной. (колонка `date` в таблице имеет ограничение not null)
|
||||||
const trx = new mssql.Transaction(db);
|
const trx = new mssql.Transaction(db);
|
||||||
const reports = await reportRepository.getAll();
|
const reports = await reportRepository.getAll();
|
||||||
|
|
||||||
/**
|
|
||||||
* 3. установить связь добавленных отчетов с исполнителями и завершить транзакцию
|
|
||||||
*/
|
|
||||||
|
|
||||||
/** Отфильтровывание дубликатов по полям. */
|
/** Отфильтровывание дубликатов по полям. */
|
||||||
const filteredInReports = inReports.filter((inputR) => {
|
const filteredInReports = inReports.filter((inputR) => {
|
||||||
const isDub = reports.some((dbR) => {
|
const isDub = reports.some((dbR) => {
|
||||||
@ -198,15 +195,16 @@ export const reportsService = {
|
|||||||
|
|
||||||
if (filteredInReports.length === 0) return 0;
|
if (filteredInReports.length === 0) return 0;
|
||||||
|
|
||||||
|
/** Добавление отчетов и связей с исполнителями к ним */
|
||||||
try {
|
try {
|
||||||
await trx.begin();
|
await trx.begin();
|
||||||
const countRows = await reportRepository.bulk(
|
const bulkResult = await reportRepository.bulk(
|
||||||
filteredInReports.map((r) => {
|
filteredInReports.map((r) => {
|
||||||
const reportDate = new Date(r.date || "");
|
const reportDate = new Date(r.date || "");
|
||||||
return {
|
return {
|
||||||
applicant_id: r.applicant?.id || null,
|
applicant_id: r.applicant?.id || null,
|
||||||
equipment_id: r.equipment?.id || null,
|
equipment_id: r.equipment?.id || null,
|
||||||
date: utilsDate.isDateValid(reportDate) ? reportDate : new Date("2016-10-24"),
|
date: utilsDate.isDateValid(reportDate) ? reportDate : INVALID_DATE_FALLBACK,
|
||||||
job_description: r.job_description || "",
|
job_description: r.job_description || "",
|
||||||
reason_call: r.reason_call || "",
|
reason_call: r.reason_call || "",
|
||||||
root_cause: r.root_cause || ""
|
root_cause: r.root_cause || ""
|
||||||
@ -214,10 +212,14 @@ export const reportsService = {
|
|||||||
}),
|
}),
|
||||||
trx
|
trx
|
||||||
);
|
);
|
||||||
// filteredInReports.map((r) => ({ [r.]: "" }));
|
|
||||||
// await reportRepository.attachExecutors()
|
const executorsLinks = filteredInReports.map((r, i) => {
|
||||||
|
return Object.fromEntries([[`${bulkResult.addedRowIds[i]}`, r.executors.map((e) => e.id)]]);
|
||||||
|
});
|
||||||
|
|
||||||
|
await reportRepository.attachExecutors(executorsLinks, trx);
|
||||||
await trx.commit();
|
await trx.commit();
|
||||||
return countRows;
|
return bulkResult.rowsAffected;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
await trx.rollback();
|
await trx.rollback();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user