add: add notification
This commit is contained in:
@@ -1,12 +1,26 @@
|
|||||||
const NotificationErrorService = require('../services/notification_error.service');
|
const NotificationErrorService = require("../services/notification_error.service");
|
||||||
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
|
const {
|
||||||
|
setResponse,
|
||||||
|
setResponsePaging,
|
||||||
|
checkValidate,
|
||||||
|
} = require("../helpers/utils");
|
||||||
|
const {
|
||||||
|
insertNotificationSchema,
|
||||||
|
updateNotificationSchema,
|
||||||
|
} = require("../validate/notification.schema");
|
||||||
|
|
||||||
class NotificationErrorController {
|
class NotificationErrorController {
|
||||||
static async getAll(req, res) {
|
static async getAll(req, res) {
|
||||||
const queryParams = req.query;
|
const queryParams = req.query;
|
||||||
|
|
||||||
const results = await NotificationErrorService.getAllNotification(queryParams);
|
const results = await NotificationErrorService.getAllNotification(
|
||||||
const response = await setResponsePaging(queryParams, results, 'Notification found')
|
queryParams
|
||||||
|
);
|
||||||
|
const response = await setResponsePaging(
|
||||||
|
queryParams,
|
||||||
|
results,
|
||||||
|
"Notification found"
|
||||||
|
);
|
||||||
|
|
||||||
res.status(response.statusCode).json(response);
|
res.status(response.statusCode).json(response);
|
||||||
}
|
}
|
||||||
@@ -15,11 +29,30 @@ class NotificationErrorController {
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
const results = await NotificationErrorService.getNotificationById(id);
|
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);
|
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;
|
module.exports = NotificationErrorController;
|
||||||
@@ -1,44 +1,14 @@
|
|||||||
const pool = require("../config");
|
const pool = require("../config");
|
||||||
|
|
||||||
const InsertNotificationErrorDb = async () => {
|
const InsertNotificationErrorDb = async (store) => {
|
||||||
const insertQuery = `
|
const { query: queryText, values } = pool.buildDynamicInsert(
|
||||||
INSERT INTO notification_error (
|
"notification_error",
|
||||||
error_code_id,
|
store
|
||||||
is_active,
|
);
|
||||||
is_delivered,
|
const result = await pool.query(queryText, values);
|
||||||
is_read,
|
const insertedId = result.recordset?.[0]?.inserted_id;
|
||||||
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,
|
|
||||||
|
|
||||||
CONCAT(
|
return insertedId ? await getNotificationByIdDb(insertedId) : null;
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getNotificationByIdDb = async (id) => {
|
const getNotificationByIdDb = async (id) => {
|
||||||
@@ -55,8 +25,6 @@ const getNotificationByIdDb = async (id) => {
|
|||||||
const getAllNotificationDb = async (searchParams = {}) => {
|
const getAllNotificationDb = async (searchParams = {}) => {
|
||||||
let queryParams = [];
|
let queryParams = [];
|
||||||
|
|
||||||
await InsertNotificationErrorDb();
|
|
||||||
|
|
||||||
const boolFields = ["is_send", "is_delivered", "is_read", "is_active"];
|
const boolFields = ["is_send", "is_delivered", "is_read", "is_active"];
|
||||||
|
|
||||||
boolFields.forEach((f) => {
|
boolFields.forEach((f) => {
|
||||||
@@ -158,4 +126,5 @@ const getAllNotificationDb = async (searchParams = {}) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
getNotificationByIdDb,
|
getNotificationByIdDb,
|
||||||
getAllNotificationDb,
|
getAllNotificationDb,
|
||||||
|
InsertNotificationErrorDb
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,10 +7,14 @@ const router = express.Router();
|
|||||||
|
|
||||||
router
|
router
|
||||||
.route('/')
|
.route('/')
|
||||||
.get(verifyToken.verifyAccessToken, NotificationErrorController.getAll)
|
.get(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.getAll)
|
||||||
|
|
||||||
|
router
|
||||||
|
.route('/')
|
||||||
|
.post(verifyToken.verifyAccessToken,verifyAccess(), NotificationErrorController.create)
|
||||||
|
|
||||||
router
|
router
|
||||||
.route('/:id')
|
.route('/:id')
|
||||||
.get(verifyToken.verifyAccessToken, NotificationErrorController.getById)
|
.get(verifyToken.verifyAccessToken, verifyAccess(), NotificationErrorController.getById)
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const {
|
const {
|
||||||
getAllNotificationDb,
|
getAllNotificationDb,
|
||||||
getNotificationByIdDb,
|
getNotificationByIdDb,
|
||||||
|
InsertNotificationErrorDb,
|
||||||
} = require('../db/notification_error.db');
|
} = require('../db/notification_error.db');
|
||||||
|
|
||||||
const {
|
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
|
// Get notification by ID
|
||||||
static async getNotificationById(id) {
|
static async getNotificationById(id) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ const insertNotificationSchema = Joi.object({
|
|||||||
"number.base": "error_code_id must be a number",
|
"number.base": "error_code_id must be a number",
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
message_error_issue: Joi.string().max(255).optional(),
|
||||||
|
|
||||||
is_send: Joi.boolean().required().messages({
|
is_send: Joi.boolean().required().messages({
|
||||||
"any.required": "is_send is required",
|
"any.required": "is_send is required",
|
||||||
"boolean.base": "is_send must be a boolean",
|
"boolean.base": "is_send must be a boolean",
|
||||||
|
|||||||
Reference in New Issue
Block a user