added class for children validation and deletion
This commit is contained in:
parent
f680bb87fb
commit
dfa1c93450
|
|
@ -0,0 +1,33 @@
|
|||
import { cascadeSoftDeleteQuery, hasChildrenQuery } from "../repositories/hasChildrenRepository.js";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
const hasChildrenModel = (conn, { adapter, dbName, table1, fieldName1, uuidTable1, table2, fieldName2 }) => {
|
||||
const params = {
|
||||
dbName,
|
||||
table1,
|
||||
fieldName1,
|
||||
uuidTable1,
|
||||
table2,
|
||||
fieldName2
|
||||
};
|
||||
|
||||
return adapter.execute(hasChildrenQuery(params), conn, params);
|
||||
};
|
||||
|
||||
|
||||
const cascadeSoftDeleteModel = (conn, {adapter, dbName, tableName, uuid}, deleted) => {
|
||||
const deletedData = deleted ? dayjs.utc(deleted).format('YYYY-MM-DD HH:mm:ss') : dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||
const params = {
|
||||
dbName,
|
||||
tableName,
|
||||
uuid,
|
||||
deleted: deletedData}
|
||||
|
||||
return adapter
|
||||
.execute(cascadeSoftDeleteQuery(params), conn, params)
|
||||
}
|
||||
|
||||
export {
|
||||
hasChildrenModel,
|
||||
cascadeSoftDeleteModel
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
const hasChildrenQuery = ({dbName, table1, fieldName1, table2, fieldName2}) => {
|
||||
const table2InSchema = `${dbName}.${table2}`
|
||||
const table1InSchema = `${dbName}.${table1}`
|
||||
const fieldName1NoQuotation = fieldName1.replace(/['"]/g, '');
|
||||
const fieldName2NoQuotation = fieldName2.replace(/['"]/g, '');
|
||||
return `
|
||||
SELECT *
|
||||
FROM ${table2InSchema}
|
||||
WHERE ${fieldName2NoQuotation} =
|
||||
(SELECT ${fieldName1NoQuotation} FROM ${table1InSchema} WHERE uuid = :uuidTable1)
|
||||
AND deleted IS NULL
|
||||
`
|
||||
}
|
||||
|
||||
const cascadeSoftDeleteQuery = ({dbName, tableName}) => {
|
||||
const tableInSchema = `${dbName}.${tableName}`
|
||||
return `
|
||||
UPDATE ${tableInSchema}
|
||||
SET deleted = :deleted
|
||||
WHERE uuid = :uuid
|
||||
AND deleted IS NULL
|
||||
`
|
||||
}
|
||||
|
||||
export {
|
||||
hasChildrenQuery,
|
||||
cascadeSoftDeleteQuery
|
||||
}
|
||||
|
|
@ -1,21 +1,57 @@
|
|||
import { hasChildrenModel } from '../models/hasChildrenModel'
|
||||
import { cascadeSoftDeleteModel } from '../models/hasChildrenModel.js'
|
||||
import { hasChildrenModel } from '../models/hasChildrenModel.js'
|
||||
import { errorHandler } from './errors.js'
|
||||
|
||||
const hasChildren = (req, res, next, config, { adapter, schema, table1, fieldNameTable1, uuidTable1, table2, fieldNameTable2 }) => {
|
||||
const hasChildren = (req, res, next, config, { adapter, dbName, table1, fieldName1, uuidTable1, table2, fieldName2 }) => {
|
||||
return new Promise((resolve) => {
|
||||
const conn = adapter.start(config)
|
||||
resolve(hasChildrenModel({ adapter, schema, table1, fieldNameTable1, uuidTable1, table2, fieldNameTable2 }, conn)
|
||||
.then((hasChildren) => {
|
||||
const result = hasChildren === undefined || hasChildren.length === 0 ? [] : { hasChildren }
|
||||
next(result)
|
||||
return result
|
||||
})
|
||||
.catch((err) => {
|
||||
throw (err.message)
|
||||
})
|
||||
.finally(() => {
|
||||
adapter.end(conn)
|
||||
}))
|
||||
const conn = adapter.start(config)
|
||||
resolve(hasChildrenModel(conn, { adapter, dbName, table1, fieldName1, uuidTable1, table2, fieldName2 })
|
||||
.then((hasChildren) => {
|
||||
const result = hasChildren === undefined || hasChildren.length === 0 ? [] : { hasChildren }
|
||||
next(result)
|
||||
return result
|
||||
})
|
||||
.catch((err) => {
|
||||
const error = errorHandler(err, config.environment)
|
||||
res.status(error.code).json(error);
|
||||
})
|
||||
.finally(() => {
|
||||
adapter.end(conn)
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
const checkAndSoftDeleteChildren = (req, res, next, config, {adapter, dbName, childTable, childField, parentTable, parentField, parentUuid}) => {
|
||||
hasChildren(req, res, (result) => {
|
||||
const children = result.hasChildren || [];
|
||||
if (!children.length) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
const conn = adapter.start(config);
|
||||
|
||||
const deletePromises = children.map((child) => {
|
||||
return cascadeSoftDeleteModel(conn, {
|
||||
adapter,
|
||||
dbName,
|
||||
tableName: childTable,
|
||||
uuid: child.uuid
|
||||
});
|
||||
});
|
||||
|
||||
export { hasChildren }
|
||||
Promise.all(deletePromises)
|
||||
.then(() =>
|
||||
next())
|
||||
.catch(err => {
|
||||
const error = errorHandler(err, config.environment);
|
||||
res.status(error.code).json(error);
|
||||
})
|
||||
.finally(() => {
|
||||
adapter.end(conn);
|
||||
});
|
||||
}, config, { adapter, dbName, table1: parentTable, fieldName1: parentField, uuidTable1: parentUuid, table2: childTable, fieldName2: childField });
|
||||
};
|
||||
|
||||
export{
|
||||
checkAndSoftDeleteChildren, hasChildren
|
||||
}
|
||||
Loading…
Reference in New Issue