add: CRUD schedule
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
const ScheduleService = require('../services/schedule.service');
|
||||||
|
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
||||||
|
const { updateScheduleSchema, insertScheduleSchema } = require('../validate/schedule.schema');
|
||||||
|
|
||||||
|
class ScheduleController {
|
||||||
|
// Get all Schedule
|
||||||
|
static async getAll(req, res) {
|
||||||
|
const queryParams = req.query;
|
||||||
|
|
||||||
|
const results = await ScheduleService.getAllSchedule(queryParams);
|
||||||
|
const response = await setResponsePaging(queryParams, results, 'Schedule found')
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Schedule by ID
|
||||||
|
static async getById(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const results = await ScheduleService.getScheduleById(id);
|
||||||
|
const response = await setResponse(results, 'Schedule found')
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Schedule
|
||||||
|
static async create(req, res) {
|
||||||
|
const { error, value } = await checkValidate(insertScheduleSchema, req)
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||||
|
}
|
||||||
|
|
||||||
|
value.userId = req.user.user_id
|
||||||
|
|
||||||
|
const results = await ScheduleService.insertScheduleDb(value);
|
||||||
|
const response = await setResponse(results, 'Schedule created successfully')
|
||||||
|
|
||||||
|
return res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Schedule
|
||||||
|
static async update(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const { error, value } = checkValidate(updateScheduleSchema, req)
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return res.status(400).json(setResponse(error, 'Validation failed', 400));
|
||||||
|
}
|
||||||
|
|
||||||
|
value.userId = req.user.user_id
|
||||||
|
|
||||||
|
const results = await ScheduleService.updateSchedule(id, value);
|
||||||
|
const response = await setResponse(results, 'Schedule updated successfully')
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Soft delete Schedule
|
||||||
|
static async delete(req, res) {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
const results = await ScheduleService.deleteSchedule(id, req.user.user_id);
|
||||||
|
const response = await setResponse(results, 'Schedule deleted successfully')
|
||||||
|
|
||||||
|
res.status(response.statusCode).json(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ScheduleController;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
const formattedDate = require("../utils/date");
|
// const formattedDate = require("../utils/date");
|
||||||
|
|
||||||
// Get all schedules
|
// Get all schedules
|
||||||
const getAllScheduleDb = async (searchParams = {}) => {
|
const getAllScheduleDb = async (searchParams = {}) => {
|
||||||
@@ -27,15 +27,11 @@ const getAllScheduleDb = async (searchParams = {}) => {
|
|||||||
SELECT
|
SELECT
|
||||||
COUNT(*) OVER() AS total_data,
|
COUNT(*) OVER() AS total_data,
|
||||||
a.*,
|
a.*,
|
||||||
b.shift_id,
|
|
||||||
b.shift_name,
|
b.shift_name,
|
||||||
b.start_time,
|
b.start_time,
|
||||||
b.end_time,
|
b.end_time
|
||||||
c.user_schedule_id,
|
|
||||||
c.user_id
|
|
||||||
FROM schedule a
|
FROM schedule a
|
||||||
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
|
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
|
||||||
LEFT JOIN user_schedule c ON a.schedule_id = c.user_schedule_id
|
|
||||||
WHERE a.deleted_at IS NULL
|
WHERE a.deleted_at IS NULL
|
||||||
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
||||||
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
||||||
@@ -44,47 +40,36 @@ const getAllScheduleDb = async (searchParams = {}) => {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const result = await pool.query(queryText, queryParams);
|
const result = await pool.query(queryText, queryParams);
|
||||||
|
|
||||||
const data = result.recordset.map((item) => ({
|
|
||||||
...item,
|
|
||||||
schedule_date: item.schedule_date
|
|
||||||
? formattedDate(item.schedule_date)
|
|
||||||
: null,
|
|
||||||
}));
|
|
||||||
|
|
||||||
const total =
|
const total =
|
||||||
result?.recordset?.length > 0
|
result?.recordset?.length > 0
|
||||||
? parseInt(result.recordset[0].total_data, 10)
|
? parseInt(result.recordset[0].total_data, 10)
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
return { data, total };
|
return { data: result.recordset, total };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getScheduleByIdDb = async (id) => {
|
const getScheduleByIdDb = async (id) => {
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT
|
||||||
a.*,
|
a.*,
|
||||||
b.shift_id,
|
|
||||||
b.shift_name,
|
b.shift_name,
|
||||||
b.start_time,
|
b.start_time,
|
||||||
b.end_time,
|
b.end_time
|
||||||
c.user_schedule_id,
|
|
||||||
c.user_id
|
|
||||||
FROM schedule a
|
FROM schedule a
|
||||||
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
|
LEFT JOIN m_shift b ON a.shift_id = b.shift_id
|
||||||
LEFT JOIN user_schedule c ON a.schedule_id = c.user_schedule_id
|
|
||||||
WHERE a.schedule_id = $1 AND a.deleted_at IS NULL
|
WHERE a.schedule_id = $1 AND a.deleted_at IS NULL
|
||||||
`;
|
`;
|
||||||
const result = await pool.query(queryText, [id]);
|
const result = await pool.query(queryText, [id]);
|
||||||
|
|
||||||
const data = result.recordset.map((item) => ({
|
return result.recordset?.[0] || null;
|
||||||
...item,
|
};
|
||||||
schedule_date: item.schedule_date
|
|
||||||
? formattedDate(item.schedule_date)
|
|
||||||
: null,
|
|
||||||
}));
|
|
||||||
|
|
||||||
return data;
|
const insertScheduleDb = async (store) => {
|
||||||
|
const { query: queryText, values } = pool.buildDynamicInsert("schedule", store);
|
||||||
|
const result = await pool.query(queryText, values);
|
||||||
|
const insertedId = result.recordset?.[0]?.inserted_id;
|
||||||
|
|
||||||
|
return insertedId ? await getScheduleByIdDb(insertedId) : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateScheduleDb = async (id, data) => {
|
const updateScheduleDb = async (id, data) => {
|
||||||
@@ -115,6 +100,7 @@ const deleteScheduleDb = async (id, deletedBy) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
getAllScheduleDb,
|
getAllScheduleDb,
|
||||||
getScheduleByIdDb,
|
getScheduleByIdDb,
|
||||||
|
insertScheduleDb,
|
||||||
updateScheduleDb,
|
updateScheduleDb,
|
||||||
deleteScheduleDb,
|
deleteScheduleDb,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const roles = require('./roles.route')
|
|||||||
const tags = require("./tags.route")
|
const tags = require("./tags.route")
|
||||||
const subSection = require("./sub_section.route")
|
const subSection = require("./sub_section.route")
|
||||||
const shift = require("./shift.route")
|
const shift = require("./shift.route")
|
||||||
|
const schedule = require("./schedule.route")
|
||||||
|
|
||||||
router.use("/auth", auth);
|
router.use("/auth", auth);
|
||||||
router.use("/user", users);
|
router.use("/user", users);
|
||||||
@@ -14,6 +15,7 @@ router.use("/roles", roles);
|
|||||||
router.use("/tags", tags);
|
router.use("/tags", tags);
|
||||||
router.use("/plant-sub-section", subSection);
|
router.use("/plant-sub-section", subSection);
|
||||||
router.use("/shift", shift)
|
router.use("/shift", shift)
|
||||||
|
router.use("/schedule", schedule)
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const ScheduleController = require('../controllers/schedule.controller');
|
||||||
|
const verifyToken = require("../middleware/verifyToken")
|
||||||
|
const verifyAccess = require("../middleware/verifyAccess")
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.route("/")
|
||||||
|
.get(verifyToken.verifyAccessToken, ScheduleController.getAll)
|
||||||
|
.post(verifyToken.verifyAccessToken, verifyAccess(), ScheduleController.create);
|
||||||
|
|
||||||
|
router.route("/:id")
|
||||||
|
.get(verifyToken.verifyAccessToken, ScheduleController.getById)
|
||||||
|
.put(verifyToken.verifyAccessToken, verifyAccess(), ScheduleController.update)
|
||||||
|
.delete(verifyToken.verifyAccessToken, verifyAccess(), ScheduleController.delete);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
const {
|
||||||
|
getAllScheduleDb,
|
||||||
|
getScheduleByIdDb,
|
||||||
|
insertScheduleDb,
|
||||||
|
updateScheduleDb,
|
||||||
|
deleteScheduleDb
|
||||||
|
} = require('../db/schedule.db');
|
||||||
|
const { ErrorHandler } = require('../helpers/error');
|
||||||
|
|
||||||
|
class ScheduleService {
|
||||||
|
// Get all Schedule
|
||||||
|
static async getAllSchedule(param) {
|
||||||
|
try {
|
||||||
|
const results = await getAllScheduleDb(param);
|
||||||
|
|
||||||
|
results.data.map(element => {
|
||||||
|
});
|
||||||
|
|
||||||
|
return results
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Schedule by ID
|
||||||
|
static async getScheduleById(id) {
|
||||||
|
try {
|
||||||
|
const result = await getScheduleByIdDb(id);
|
||||||
|
|
||||||
|
if (result.length < 1) throw new ErrorHandler(404, 'Schedule not found');
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Schedule
|
||||||
|
static async insertScheduleDb(data) {
|
||||||
|
try {
|
||||||
|
if (!data || typeof data !== 'object') data = {};
|
||||||
|
|
||||||
|
const result = await insertScheduleDb(data);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Schedule
|
||||||
|
static async updateSchedule(id, data) {
|
||||||
|
try {
|
||||||
|
if (!data || typeof data !== 'object') data = {};
|
||||||
|
|
||||||
|
const dataExist = await getScheduleByIdDb(id);
|
||||||
|
|
||||||
|
if (dataExist.length < 1) {
|
||||||
|
throw new ErrorHandler(404, 'Schedule not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await updateScheduleDb(id, data);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Soft delete Schedule
|
||||||
|
static async deleteSchedule(id, userId) {
|
||||||
|
try {
|
||||||
|
const dataExist = await getScheduleByIdDb(id);
|
||||||
|
|
||||||
|
if (dataExist.length < 1) {
|
||||||
|
throw new ErrorHandler(404, 'Schedule not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await deleteScheduleDb(id, userId);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
throw new ErrorHandler(error.statusCode, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ScheduleService;
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
// ========================
|
||||||
|
// Schedule Validation
|
||||||
|
|
||||||
|
const Joi = require("joi");
|
||||||
|
|
||||||
|
const datePattern = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
|
||||||
|
|
||||||
|
const insertScheduleSchema = Joi.object({
|
||||||
|
schedule_date: Joi.string().pattern(datePattern).required().messages({
|
||||||
|
"string.pattern.base": "schedule_date harus dalam format YYYY-MM-DD",
|
||||||
|
"any.required": "schedule_date wajib diisi",
|
||||||
|
}),
|
||||||
|
is_active: Joi.boolean().required(),
|
||||||
|
shift_id: Joi.number(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const updateScheduleSchema = Joi.object({
|
||||||
|
schedule_date: Joi.string().pattern(datePattern).messages({
|
||||||
|
"string.pattern.base": "schedule_date harus dalam format YYYY-MM-DD",
|
||||||
|
}),
|
||||||
|
is_active: Joi.boolean(),
|
||||||
|
shift_id: Joi.number(),
|
||||||
|
}).min(1);
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
insertScheduleSchema,
|
||||||
|
updateScheduleSchema,
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user