added report types and coordinates model and controller
This commit is contained in:
parent
6c31d90278
commit
c22a8d0fbe
|
|
@ -0,0 +1,146 @@
|
||||||
|
import {
|
||||||
|
getCoordinatesListModel,
|
||||||
|
countCoordinatesListModel,
|
||||||
|
insertCoordinatesModel,
|
||||||
|
modifyCoordinatesModel,
|
||||||
|
softDeleteCoordinatesModel
|
||||||
|
} from "../../models/resource_types/coordinatesModel";
|
||||||
|
import { error404 } from "../../utils/errors";
|
||||||
|
import { sendResponseNotFound } from "../../utils/responses";
|
||||||
|
import { noResults } from "../../validators/result-validators";
|
||||||
|
import mysql from "../../adapters/mysql.js";
|
||||||
|
import { errorHandler } from "../../utils/errors";
|
||||||
|
|
||||||
|
const getCoordinatesListController = (req, res, next, config) => {
|
||||||
|
const conn = mysql.start(config)
|
||||||
|
|
||||||
|
Promise.all([
|
||||||
|
getCoordinatesListModel({...req.query, conn}),
|
||||||
|
countCoordinatesListModel({...req.query, conn})
|
||||||
|
])
|
||||||
|
.then(([getResults, countResults]) => {
|
||||||
|
next({
|
||||||
|
_data: {coordinates: getResults},
|
||||||
|
_page: {
|
||||||
|
totalElements: countResults,
|
||||||
|
limit: req.query.limit || 100,
|
||||||
|
page: req.query.page || (countResults && 1) || 0
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return res.status(error.code).json(error)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const getCoordinatesByUuidController = (req, res, next, config) => {
|
||||||
|
const uuid_coordinates = req.params.uuid
|
||||||
|
const conn = mysql.start(config)
|
||||||
|
|
||||||
|
getCoordinatesListModel({ uuid_coordinates, conn })
|
||||||
|
.then((response) => {
|
||||||
|
if (noResults(response)) {
|
||||||
|
const err = error404()
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return sendResponseNotFound(res, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
coordinates: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
res.status(error.code).json(error)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const postCoordinatesController = (req, res, next, config) => {
|
||||||
|
const conn = mysql.start(config)
|
||||||
|
|
||||||
|
insertCoordinatesModel({ ...req.body, conn })
|
||||||
|
.then((response) => {
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
coordinates: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return res.status(error.code).json(error)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const putCoordinatesController = (req, res, next, config) => {
|
||||||
|
const uuid_coordinates = req.params.uuid
|
||||||
|
const conn = mysql.start(config)
|
||||||
|
|
||||||
|
modifyCoordinatesModel({ ...req.body, uuid: uuid_coordinates, conn })
|
||||||
|
.then((response) => {
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
coordinates: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return res.status(error.code).json(error)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteCoordinatesController = (req, res, next, config) => {
|
||||||
|
const uuid_coordinates = req.params.uuid
|
||||||
|
const conn = mysql.start(config)
|
||||||
|
|
||||||
|
softDeleteCoordinatesModel({ uuid: uuid_coordinates, conn })
|
||||||
|
.then((response) => {
|
||||||
|
if (noResults(response)) {
|
||||||
|
const err = error404()
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return sendResponseNotFound(res, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
coordinates: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result)
|
||||||
|
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment)
|
||||||
|
return res.status(error.code).json(error)
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
getCoordinatesListController,
|
||||||
|
getCoordinatesByUuidController,
|
||||||
|
postCoordinatesController,
|
||||||
|
putCoordinatesController,
|
||||||
|
deleteCoordinatesController
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,137 @@
|
||||||
|
import mysql from "../../adapters/mysql.js";
|
||||||
|
import { errorHandler } from "../../utils/errors.js";
|
||||||
|
import { error404 } from "../../utils/errors.js";
|
||||||
|
import { sendResponseNotFound } from "../../utils/responses.js";
|
||||||
|
import { noResults } from "../../validators/result-validators.js";
|
||||||
|
import {
|
||||||
|
getReportTypesListModel,
|
||||||
|
countReportTypesListModel,
|
||||||
|
insertReportTypesListModel,
|
||||||
|
modifyReportTypesListModel,
|
||||||
|
softDeleteReportTypesListModel
|
||||||
|
} from "../../models/resource_types/reportTypesModel.js";
|
||||||
|
|
||||||
|
const getReportTypesListController = (req, res, next, config) => {
|
||||||
|
const conn = mysql.start(config);
|
||||||
|
|
||||||
|
Promise.all([
|
||||||
|
getReportTypesListModel({ ...req.query, conn }),
|
||||||
|
countReportTypesListModel({ ...req.query, conn })
|
||||||
|
])
|
||||||
|
.then(([getResults, countResults]) => {
|
||||||
|
next({
|
||||||
|
_data: { report_types: getResults },
|
||||||
|
_page: {
|
||||||
|
totalElements: countResults,
|
||||||
|
limit: req.query.limit || 100,
|
||||||
|
page: req.query.page || (countResults && 1) || 0
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment);
|
||||||
|
return res.status(error.code).json(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const getReportTypesByUuidController = (req, res, next, config) => {
|
||||||
|
const uuid_report_type = req.params.uuid;
|
||||||
|
const conn = mysql.start(config);
|
||||||
|
|
||||||
|
getReportTypesListModel({ uuid: uuid_report_type, conn })
|
||||||
|
.then((response) => {
|
||||||
|
if (noResults(response)) {
|
||||||
|
const err = error404();
|
||||||
|
const error = errorHandler(err, config.environment);
|
||||||
|
return sendResponseNotFound(res, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
report_types: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment);
|
||||||
|
return res.status(error.code).json(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const postReportTypesController = (req, res, next, config) => {
|
||||||
|
const conn = mysql.start(config);
|
||||||
|
const created_by = req.auth.user || null
|
||||||
|
|
||||||
|
insertReportTypesListModel({ ...req.body, created_by, conn })
|
||||||
|
.then((response) => {
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
report_types: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment);
|
||||||
|
return res.status(error.code).json(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const putReportTypesController = (req, res, next, config) => {
|
||||||
|
const conn = mysql.start(config);
|
||||||
|
const uuid_report_type = req.params.uuid;
|
||||||
|
|
||||||
|
modifyReportTypesListModel({ ...req.body, uuid: uuid_report_type, conn })
|
||||||
|
.then((response) => {
|
||||||
|
const result = {
|
||||||
|
_data: {
|
||||||
|
report_types: response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next(result);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment);
|
||||||
|
return res.status(error.code).json(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const softDeleteReportTypesController = (req, res, next, config) => {
|
||||||
|
const conn = mysql.start(config);
|
||||||
|
const uuid_report_type = req.params.uuid;
|
||||||
|
const deleted_by = req.auth.user || null
|
||||||
|
|
||||||
|
softDeleteReportTypesListModel({ uuid: uuid_report_type, deleted_by, conn })
|
||||||
|
.then(() => {
|
||||||
|
const result = {}
|
||||||
|
next(result);
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
const error = errorHandler(err, config.environment);
|
||||||
|
return res.status(error.code).json(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
mysql.end(conn);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
getReportTypesListController,
|
||||||
|
getReportTypesByUuidController,
|
||||||
|
postReportTypesController,
|
||||||
|
putReportTypesController,
|
||||||
|
softDeleteReportTypesController
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
import {
|
||||||
|
getCoordinatesListQuery,
|
||||||
|
countCoordinatesListQuery,
|
||||||
|
insertCoordinatesQuery,
|
||||||
|
updateCoordinatesQuery,
|
||||||
|
softDeleteCoordinatesQuery
|
||||||
|
} from "../../repositories/resource_types/coordinatesRepository";
|
||||||
|
import { randomUUID as uuidv4 } from 'node:crypto'
|
||||||
|
import dayjs from 'dayjs'
|
||||||
|
import mysql from '../../adapters/mysql.js'
|
||||||
|
import { error404 } from '../../utils/errors.js'
|
||||||
|
|
||||||
|
const getCoordinatesListModel = ({conn, ...rest}) => {
|
||||||
|
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const paramsToSearch = {...rest, now}
|
||||||
|
return mysql
|
||||||
|
.execute(getCoordinatesListQuery(paramsToSearch), conn, paramsToSearch)
|
||||||
|
.then(queryResult => queryResult.map(({id, created, deleted, createdBy, deletedBy, ...resultFiltered}) => resultFiltered))
|
||||||
|
}
|
||||||
|
|
||||||
|
const countCoordinatesListModel = ({conn, ...rest}) => {
|
||||||
|
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const paramsToSearch = {...rest, now}
|
||||||
|
return mysql
|
||||||
|
.execute(countCoordinatesListQuery(paramsToSearch), conn, paramsToSearch)
|
||||||
|
.then(results => results[0].count)
|
||||||
|
}
|
||||||
|
|
||||||
|
const insertCoordinatesModel = ({conn, ...params}) => {
|
||||||
|
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const uuid = uuidv4()
|
||||||
|
return mysql
|
||||||
|
.execute(insertCoordinatesQuery({...params, uuid, now}), conn, {...params, uuid, now})
|
||||||
|
.then(queryResult => queryResult[1].map(({id, created, deleted, createdBy, deletedBy, ...resultFiltered}) => resultFiltered))
|
||||||
|
}
|
||||||
|
|
||||||
|
const modifyCoordinatesModel = ({conn, ...params}) => {
|
||||||
|
return mysql
|
||||||
|
.execute(updateCoordinatesQuery(params), conn, params)
|
||||||
|
.then(queryResult => {
|
||||||
|
const deletedItem = queryResult[1].find(item => item.deleted !== null);
|
||||||
|
|
||||||
|
if (deletedItem) {
|
||||||
|
throw error404()
|
||||||
|
}
|
||||||
|
return queryResult[1].map(({id, created, deleted, createdBy, deletedBy, ...resultFiltered}) => resultFiltered)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const softDeleteCoordinatesModel = ({uuid, deleted, deletedBy, conn}) => {
|
||||||
|
const deletedData = deleted ? dayjs.utc(deleted).format('YYYY-MM-DD HH:mm:ss') : dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const params = {uuid, deletedBy, deleted: deletedData}
|
||||||
|
return mysql
|
||||||
|
.execute(softDeleteCoordinatesQuery(params), conn, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
getCoordinatesListModel,
|
||||||
|
countCoordinatesListModel,
|
||||||
|
insertCoordinatesModel,
|
||||||
|
modifyCoordinatesModel,
|
||||||
|
softDeleteCoordinatesModel
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
import {
|
||||||
|
getReportTypesListQuery,
|
||||||
|
countReportTypesListQuery,
|
||||||
|
insertReportTypesQuery,
|
||||||
|
updateReportTypesQuery,
|
||||||
|
softDeleteReportTypesQuery,
|
||||||
|
} from "../../repositories/resource_types/reportTypesRepository";
|
||||||
|
import { randomUUID as uuidv4 } from 'node:crypto'
|
||||||
|
import dayjs from 'dayjs'
|
||||||
|
import mysql from '../../adapters/mysql.js'
|
||||||
|
import { error404 } from '../../utils/errors.js'
|
||||||
|
|
||||||
|
const getReportTypesListModel = ({conn, ...rest}) => {
|
||||||
|
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const paramsToSearch = {...rest, now}
|
||||||
|
return mysql
|
||||||
|
.execute(getReportTypesListQuery(paramsToSearch), conn, paramsToSearch)
|
||||||
|
.then(queryResult => queryResult.map(({id, created, deleted, createdBy, deletedBy, ...resultFiltered}) => resultFiltered))
|
||||||
|
}
|
||||||
|
|
||||||
|
const countReportTypesListModel = ({conn, ...rest}) => {
|
||||||
|
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const paramsToSearch = {...rest, now}
|
||||||
|
return mysql
|
||||||
|
.execute(countReportTypesListQuery(paramsToSearch), conn, paramsToSearch)
|
||||||
|
.then(results => results[0].count)
|
||||||
|
}
|
||||||
|
|
||||||
|
const insertReportTypesModel = ({conn, ...params}) => {
|
||||||
|
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const uuid = uuidv4()
|
||||||
|
return mysql
|
||||||
|
.execute(insertReportTypesQuery({...params, uuid, now}), conn, {...params, uuid, now})
|
||||||
|
.then(queryResult => queryResult[1].map(({id, created, deleted, createdBy, deletedBy, ...resultFiltered}) => resultFiltered))
|
||||||
|
}
|
||||||
|
|
||||||
|
const modifyReportTypesModel = ({conn, ...params}) => {
|
||||||
|
return mysql
|
||||||
|
.execute(updateReportTypesQuery(params), conn, params)
|
||||||
|
.then(queryResult => {
|
||||||
|
const deletedItem = queryResult[1].find(item => item.deleted !== null);
|
||||||
|
|
||||||
|
if (deletedItem) {
|
||||||
|
throw error404()
|
||||||
|
}
|
||||||
|
return queryResult[1].map(({id, created, deleted, createdBy, deletedBy, ...resultFiltered}) => resultFiltered)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const softDeleteReportTypesModel = ({uuid, deleted, deletedBy, conn}) => {
|
||||||
|
const deletedData = deleted ? dayjs.utc(deleted).format('YYYY-MM-DD HH:mm:ss') : dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
|
||||||
|
const params = {uuid, deletedBy, deleted: deletedData}
|
||||||
|
return mysql
|
||||||
|
.execute(softDeleteReportTypesQuery(params), conn, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
getReportTypesListModel,
|
||||||
|
countReportTypesListModel,
|
||||||
|
insertReportTypesModel,
|
||||||
|
modifyReportTypesModel,
|
||||||
|
softDeleteReportTypesModel
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue