add: add notification

This commit is contained in:
2025-12-09 16:04:32 +07:00
parent d063478fc2
commit fb3061e0d1
5 changed files with 69 additions and 48 deletions

View File

@@ -1,12 +1,26 @@
const NotificationErrorService = require('../services/notification_error.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const NotificationErrorService = require("../services/notification_error.service");
const {
setResponse,
setResponsePaging,
checkValidate,
} = require("../helpers/utils");
const {
insertNotificationSchema,
updateNotificationSchema,
} = require("../validate/notification.schema");
class NotificationErrorController {
static async getAll(req, res) {
const queryParams = req.query;
const results = await NotificationErrorService.getAllNotification(queryParams);
const response = await setResponsePaging(queryParams, results, 'Notification found')
const results = await NotificationErrorService.getAllNotification(
queryParams
);
const response = await setResponsePaging(
queryParams,
results,
"Notification found"
);
res.status(response.statusCode).json(response);
}
@@ -15,11 +29,30 @@ class NotificationErrorController {
const { id } = req.params;
const results = await NotificationErrorService.getNotificationById(id);
const response = await setResponse(results, 'Notification retrieved successfully');
const response = await setResponse(
results,
"Notification retrieved successfully"
);
return res.status(response.statusCode).json(response);
}
static async create(req, res) {
const { error, value } = await checkValidate(insertNotificationSchema, req);
if (error) {
return res.status(400).json(setResponse(error, "Validation failed", 400));
}
value.userId = req.user.user_id;
const results = await NotificationErrorService.createNotificationError(
value
);
const response = await setResponse(results, "Notification created successfully");
return res.status(response.statusCode).json(response);
}
}
module.exports = NotificationErrorController;

View File

@@ -1,44 +1,14 @@
const pool = require("../config");
const InsertNotificationErrorDb = async () => {
const insertQuery = `
INSERT INTO notification_error (
error_code_id,
is_active,
is_delivered,
is_read,
is_send,
message_error_issue
)
SELECT
b.error_code_id,
1 AS is_active,
1 AS is_delivered,
0 AS is_read,
1 AS is_send,
const InsertNotificationErrorDb = async (store) => {
const { query: queryText, values } = pool.buildDynamicInsert(
"notification_error",
store
);
const result = await pool.query(queryText, values);
const insertedId = result.recordset?.[0]?.inserted_id;
CONCAT(
COALESCE(b.error_code_name, '-'),
' pada ',
COALESCE(d.device_name, '-'),
'. Pengecekan potensi kerusakan dibutuhkan'
) AS message_error_issue
FROM brand_code b
LEFT JOIN notification_error a
ON a.error_code_id = b.error_code_id
AND a.deleted_at IS NULL
LEFT JOIN m_device d
ON b.brand_id = d.brand_id
AND d.deleted_at IS NULL
WHERE b.deleted_at IS NULL
AND a.notification_error_id IS NULL;
`;
await pool.query(insertQuery);
return insertedId ? await getNotificationByIdDb(insertedId) : null;
};
const getNotificationByIdDb = async (id) => {
@@ -55,8 +25,6 @@ const getNotificationByIdDb = async (id) => {
const getAllNotificationDb = async (searchParams = {}) => {
let queryParams = [];
await InsertNotificationErrorDb();
const boolFields = ["is_send", "is_delivered", "is_read", "is_active"];
boolFields.forEach((f) => {
@@ -158,4 +126,5 @@ const getAllNotificationDb = async (searchParams = {}) => {
module.exports = {
getNotificationByIdDb,
getAllNotificationDb,
InsertNotificationErrorDb
};

View File

@@ -7,10 +7,14 @@ const router = express.Router();
router
.route('/')
.get(verifyToken.verifyAccessToken, NotificationErrorController.getAll)
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll)
router
.route('/')
.post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create)
router
.route('/:id')
.get(verifyToken.verifyAccessToken, NotificationErrorController.getById)
.get(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.getById)
module.exports = router;

View File

@@ -1,6 +1,7 @@
const {
getAllNotificationDb,
getNotificationByIdDb,
InsertNotificationErrorDb,
} = require('../db/notification_error.db');
const {
@@ -40,6 +41,18 @@ class NotificationService {
}
}
static async createNotificationError(data) {
try {
if (!data || typeof data !== 'object') data = {};
const result = await InsertNotificationErrorDb(data);
return result;
} catch (error) {
throw new ErrorHandler(error.statusCode, error.message);
}
}
// Get notification by ID
static async getNotificationById(id) {
try {

View File

@@ -13,6 +13,8 @@ const insertNotificationSchema = Joi.object({
"number.base": "error_code_id must be a number",
}),
message_error_issue: Joi.string().max(255).optional(),
is_send: Joi.boolean().required().messages({
"any.required": "is_send is required",
"boolean.base": "is_send must be a boolean",