@@ -1,6 +1,6 @@
|
||||
const SubSectionService = require('../services/plant_sub_section.service');
|
||||
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
||||
const { insertSubSectionSchema, updateSubSectionSchema } = require('../validate/sub_section.schema');
|
||||
const { insertSubSectionSchema, updateSubSectionSchema } = require('../validate/plant_sub_section.schema');
|
||||
|
||||
class SubSectionController {
|
||||
// Get all sub sections
|
||||
|
||||
@@ -21,7 +21,7 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
{ column: "a.user_id", param: searchParams.user_id, type: "int" },
|
||||
{ column: "a.user_schedule_id", param: searchParams.user_schedule_id, type: "int" },
|
||||
{ column: "a.schedule_id", param: searchParams.schedule_id, type: "int" },
|
||||
{ column: "a.user_schedule_id", param: searchParams.user_schedule_id, type: "int" },
|
||||
{ column: "a.shift_id", param: searchParams.shift_id, type: "int" },
|
||||
],
|
||||
queryParams
|
||||
);
|
||||
@@ -30,12 +30,14 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
COUNT(*) OVER() AS total_data,
|
||||
a.*,
|
||||
b.schedule_date,
|
||||
a.shift_id,
|
||||
c.shift_name,
|
||||
c.start_time,
|
||||
c.end_time,
|
||||
d.user_fullname
|
||||
d.user_id,
|
||||
d.user_fullname,
|
||||
d.user_name,
|
||||
d.user_phone
|
||||
FROM user_schedule a
|
||||
LEFT JOIN schedule b ON a.schedule_id = b.schedule_id
|
||||
LEFT JOIN m_shift c ON a.shift_id = c.shift_id
|
||||
@@ -43,18 +45,57 @@ const getAllUserScheduleDb = async (searchParams = {}) => {
|
||||
WHERE a.deleted_at IS NULL
|
||||
${whereConditions.length > 0 ? ` AND ${whereConditions.join(" AND ")}` : ""}
|
||||
${whereOrConditions ? ` ${whereOrConditions}` : ""}
|
||||
ORDER BY a.user_schedule_id ASC
|
||||
ORDER BY c.shift_id ASC
|
||||
${searchParams.limit ? `OFFSET $2 * $1 ROWS FETCH NEXT $1 ROWS ONLY` : ''}
|
||||
`;
|
||||
|
||||
const result = await pool.query(queryText, queryParams);
|
||||
const total =
|
||||
result?.recordset?.length > 0
|
||||
? parseInt(result.recordset[0].total_data, 10)
|
||||
: 0;
|
||||
const records = result.recordset || [];
|
||||
|
||||
return { data: result.recordset, total };
|
||||
const total = records.length > 0 ? parseInt(records[0].total_data, 10) : 0;
|
||||
|
||||
const groupedData = Object.values(
|
||||
records.reduce((acc, row) => {
|
||||
if (!acc[row.shift_id]) {
|
||||
acc[row.shift_id] = {
|
||||
shift_id: row.shift_id,
|
||||
shift_name: row.shift_name,
|
||||
start_time: row.start_time,
|
||||
end_time: row.end_time,
|
||||
users: [],
|
||||
};
|
||||
}
|
||||
|
||||
acc[row.shift_id].users.push({
|
||||
user_id: row.user_id,
|
||||
user_fullname: row.user_fullname,
|
||||
user_name: row.user_name,
|
||||
user_phone: row.user_phone,
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, {})
|
||||
);
|
||||
|
||||
return { data: groupedData, total };
|
||||
};
|
||||
|
||||
const getUserScheduleById = async (userId, shiftId) => {
|
||||
const queryText = `
|
||||
SELECT
|
||||
a.user_schedule_id,
|
||||
a.user_id,
|
||||
a.shift_id
|
||||
FROM user_schedule a
|
||||
WHERE a.user_id = $1
|
||||
AND a.shift_id = $2
|
||||
AND a.deleted_at IS NULL
|
||||
`;
|
||||
const result = await pool.query(queryText, [userId, shiftId]);
|
||||
return result.recordset || [];
|
||||
};
|
||||
|
||||
|
||||
|
||||
const getUserScheduleByIdDb = async (id) => {
|
||||
const queryText = `
|
||||
@@ -64,7 +105,9 @@ const getUserScheduleByIdDb = async (id) => {
|
||||
c.shift_name,
|
||||
c.start_time,
|
||||
c.end_time,
|
||||
d.user_fullname
|
||||
d.user_fullname,
|
||||
d.user_name,
|
||||
d.user_phone
|
||||
FROM user_schedule a
|
||||
LEFT JOIN schedule b ON a.schedule_id = b.schedule_id
|
||||
LEFT JOIN m_shift c ON a.shift_id = c.shift_id
|
||||
@@ -107,10 +150,12 @@ const deleteUserScheduleDb = async (id, deletedBy) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
getAllUserScheduleDb,
|
||||
getUserScheduleByIdDb,
|
||||
insertUserScheduleDb,
|
||||
updateUserScheduleDb,
|
||||
deleteUserScheduleDb,
|
||||
getUserScheduleById,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const {
|
||||
getAllUserScheduleDb,
|
||||
getUserScheduleByIdDb,
|
||||
getUserScheduleById,
|
||||
insertUserScheduleDb,
|
||||
updateUserScheduleDb,
|
||||
deleteUserScheduleDb
|
||||
@@ -35,20 +36,30 @@ const {
|
||||
}
|
||||
}
|
||||
|
||||
// Create device
|
||||
static async createUserSchedules(data) {
|
||||
try {
|
||||
if (!data || typeof data !== 'object') data = {};
|
||||
|
||||
const result = await insertUserScheduleDb(data);
|
||||
const exist = await getUserScheduleById(
|
||||
data.user_id,
|
||||
data.shift_id
|
||||
);
|
||||
|
||||
if (exist.length > 0) {
|
||||
throw new ErrorHandler(
|
||||
400,
|
||||
`User dengan ID ${data.user_id} sudah terdaftar pada shift ${data.shift_id}`
|
||||
);
|
||||
}
|
||||
|
||||
const result = await insertUserScheduleDb(data);
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Update device
|
||||
// Update user schedule
|
||||
static async updateUserSchedules(id, data) {
|
||||
try {
|
||||
if (!data || typeof data !== 'object') data = {};
|
||||
@@ -59,8 +70,22 @@ const {
|
||||
throw new ErrorHandler(404, 'UserSchedules not found');
|
||||
}
|
||||
|
||||
const result = await updateUserScheduleDb(id, data);
|
||||
// 🧩 VALIDASI SAAT UPDATE
|
||||
if (data.user_id && data.shift_id) {
|
||||
const exist = await getUserScheduleById(
|
||||
data.user_id,
|
||||
data.shift_id
|
||||
);
|
||||
|
||||
if (exist.length > 0 && exist[0].id !== Number(id)) {
|
||||
throw new ErrorHandler(
|
||||
400,
|
||||
`User dengan ID ${data.user_id} sudah terdaftar pada shift ${data.shift_id}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const result = await updateUserScheduleDb(id, data);
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new ErrorHandler(error.statusCode, error.message);
|
||||
|
||||
65
validate/plant_sub_section.schema.js
Normal file
65
validate/plant_sub_section.schema.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const Joi = require("joi");
|
||||
|
||||
// ========================
|
||||
// Plant Sub Section Validation
|
||||
// ========================
|
||||
const insertSubSectionSchema = Joi.object({
|
||||
plant_sub_section_name: Joi.string()
|
||||
.max(200)
|
||||
.required()
|
||||
.messages({
|
||||
"string.base": "Sub section name must be a string",
|
||||
"string.max": "Sub section name cannot exceed 200 characters",
|
||||
"any.required": "Sub section name is required"
|
||||
}),
|
||||
plant_sub_section_description: Joi.string()
|
||||
.max(200)
|
||||
.allow('')
|
||||
.optional()
|
||||
.messages({
|
||||
"string.base": "Description must be a string",
|
||||
"string.max": "Description cannot exceed 200 characters"
|
||||
}),
|
||||
table_name_value: Joi.string()
|
||||
.max(255)
|
||||
.allow('')
|
||||
.optional()
|
||||
.messages({
|
||||
"string.base": "Table name value must be a string",
|
||||
"string.max": "Table name value cannot exceed 255 characters"
|
||||
}),
|
||||
is_active: Joi.boolean()
|
||||
.default(true)
|
||||
.optional()
|
||||
});
|
||||
|
||||
const updateSubSectionSchema = Joi.object({
|
||||
plant_sub_section_name: Joi.string()
|
||||
.max(200)
|
||||
.messages({
|
||||
"string.base": "Sub section name must be a string",
|
||||
"string.max": "Sub section name cannot exceed 200 characters",
|
||||
}).optional(),
|
||||
plant_sub_section_description: Joi.string()
|
||||
.max(200)
|
||||
.allow('')
|
||||
.optional()
|
||||
.messages({
|
||||
"string.base": "Description must be a string",
|
||||
"string.max": "Description cannot exceed 200 characters"
|
||||
}),
|
||||
table_name_value: Joi.string()
|
||||
.max(255)
|
||||
.allow('')
|
||||
.optional()
|
||||
.messages({
|
||||
"string.base": "Table name value must be a string",
|
||||
"string.max": "Table name value cannot exceed 255 characters"
|
||||
}),
|
||||
is_active: Joi.boolean().optional()
|
||||
}).min(1);
|
||||
|
||||
module.exports = {
|
||||
insertSubSectionSchema,
|
||||
updateSubSectionSchema
|
||||
};
|
||||
@@ -1,35 +0,0 @@
|
||||
const Joi = require("joi");
|
||||
|
||||
// ========================
|
||||
// Plant Sub Section Validation
|
||||
// ========================
|
||||
const insertSubSectionSchema = Joi.object({
|
||||
plant_sub_section_name: Joi.string()
|
||||
.max(200)
|
||||
.required()
|
||||
.messages({
|
||||
"string.base": "Sub section name must be a string",
|
||||
"string.max": "Sub section name cannot exceed 200 characters",
|
||||
"any.required": "Sub section name is required"
|
||||
}).required(),
|
||||
is_active: Joi.boolean().required(),
|
||||
value: Joi.string().max(100).optional()
|
||||
});
|
||||
|
||||
const updateSubSectionSchema = Joi.object({
|
||||
plant_sub_section_namesub_section_name: Joi.string()
|
||||
.max(200)
|
||||
.messages({
|
||||
"string.base": "Sub section name must be a string",
|
||||
"string.max": "Sub section name cannot exceed 200 characters",
|
||||
}).optional(),
|
||||
is_active: Joi.boolean().optional(),
|
||||
value: Joi.string().max(100).optional()
|
||||
}).min(1).messages({
|
||||
"object.min": "At least one field must be provided to update",
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
insertSubSectionSchema,
|
||||
updateSubSectionSchema
|
||||
};
|
||||
@@ -22,7 +22,7 @@ const insertTagsSchema = Joi.object({
|
||||
});
|
||||
|
||||
const updateTagsSchema = Joi.object({
|
||||
device_id: Joi.number().required(),
|
||||
device_id: Joi.number().optional(),
|
||||
tag_name: Joi.string().max(200),
|
||||
tag_number: Joi.number(),
|
||||
is_active: Joi.boolean(),
|
||||
|
||||
@@ -5,16 +5,14 @@ const Joi = require("joi");
|
||||
// ========================
|
||||
const insertUnitSchema = Joi.object({
|
||||
unit_name: Joi.string().max(100).required(),
|
||||
tag_id: Joi.number().integer().optional(),
|
||||
is_active: Joi.boolean().required(),
|
||||
unit_description: Joi. string().max(255).optional()
|
||||
unit_description: Joi.string().max(255).allow('')
|
||||
});
|
||||
|
||||
const updateUnitSchema = Joi.object({
|
||||
unit_name: Joi.string().max(100).optional(),
|
||||
tag_id: Joi.number().integer().optional(),
|
||||
is_active: Joi.boolean().optional(),
|
||||
unit_description: Joi. string().max(255).optional()
|
||||
unit_description: Joi.string().max(255).allow('')
|
||||
}).min(1);
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -5,7 +5,7 @@ const Joi = require("joi");
|
||||
// ========================
|
||||
const insertUserScheduleSchema = Joi.object({
|
||||
user_id: Joi.number().integer().required(),
|
||||
schedule_id: Joi.number().integer().required(),
|
||||
schedule_id: Joi.number().integer().optional(),
|
||||
shift_id: Joi.number().required(),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user