mirror of
https://github.com/mohamadihsan/topuponline.git
synced 2026-12-05 10:56:27 +00:00
234 lines
5.2 KiB
JavaScript
234 lines
5.2 KiB
JavaScript
const knex = require("../../../infrastructure/database/knex");
|
|
const { param, body, validationResult } = require("express-validator");
|
|
|
|
// VALIDATION
|
|
const validate = (method) => {
|
|
switch (method) {
|
|
case "createPaymentChannel": {
|
|
return [
|
|
body("payment_channel_id")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Payment Channel ID is required")
|
|
.matches("[0-9]")
|
|
.withMessage("Payment Channel ID is invalid format"),
|
|
body("payment_channel_name")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Payment Channel Name is required"),
|
|
];
|
|
}
|
|
case "updatePaymentChannel": {
|
|
return [
|
|
param("id", "ID data is invalid format").matches("[0-9]"),
|
|
body("payment_channel_id")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Payment Channel ID is required")
|
|
.matches("[0-9]")
|
|
.withMessage("Payment Channel ID is invalid format"),
|
|
body("payment_channel_name")
|
|
.not()
|
|
.isEmpty()
|
|
.withMessage("Payment Channel Name is required"),
|
|
];
|
|
}
|
|
}
|
|
};
|
|
|
|
// SHOW ALL DATA
|
|
const getPaymentChannels = async (req, res) => {
|
|
try {
|
|
let result = await knex.select().from("mtr.t_mtr_payment_channel");
|
|
if (result) {
|
|
return res.json({
|
|
code: "00",
|
|
message: "Data found.",
|
|
data: result,
|
|
});
|
|
} else {
|
|
return res.json({
|
|
code: "01",
|
|
message: "Data not found.",
|
|
data: [],
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
// SHOW DATA BY ID
|
|
const getPaymentChannelById = async (req, res) => {
|
|
try {
|
|
let id = req.params.id;
|
|
|
|
let result = await knex
|
|
.select()
|
|
.from("mtr.t_mtr_payment_channel")
|
|
.where("payment_channel_id_seq", id)
|
|
.first();
|
|
if (result) {
|
|
return res.json({
|
|
code: "00",
|
|
message: "Data found.",
|
|
data: result,
|
|
});
|
|
} else {
|
|
return res.json({
|
|
code: "01",
|
|
message: "Data not found.",
|
|
data: [],
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
// CREATED NEW DATA
|
|
const createPaymentChannel = async (req, res) => {
|
|
try {
|
|
let { payment_channel_id, payment_channel_name, logo } = req.body;
|
|
|
|
// validate
|
|
const err = validationResult(req);
|
|
if (!err.isEmpty()) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: err.errors[0].msg,
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
let find = await knex("mtr.t_mtr_payment_channel")
|
|
.where("payment_channel_id_seq", payment_channel_id)
|
|
.orWhere("payment_channel_name_var", payment_channel_name)
|
|
.first();
|
|
|
|
if (!find) {
|
|
let result = await knex("mtr.t_mtr_payment_channel").insert({
|
|
payment_channel_id_seq: payment_channel_id,
|
|
payment_channel_name_var: payment_channel_name,
|
|
logo_txt: logo,
|
|
active_boo: true,
|
|
created_by_var: "",
|
|
});
|
|
|
|
return res.json({
|
|
code: "00",
|
|
message: "Data has been saved.",
|
|
data: result.command,
|
|
});
|
|
} else {
|
|
return res.json({
|
|
code: "01",
|
|
message: "Data already exist",
|
|
data: [],
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
// UDPATE DATA BY ID
|
|
const updatePaymentChannel = async (req, res) => {
|
|
try {
|
|
let id = req.params.id;
|
|
let { payment_channel_id, payment_channel_name, logo } = req.body;
|
|
|
|
// validate
|
|
const err = validationResult(req);
|
|
if (!err.isEmpty()) {
|
|
return res.status(400).json({
|
|
code: "01",
|
|
message: err.errors[0].msg,
|
|
data: [],
|
|
});
|
|
}
|
|
|
|
let result = await knex("mtr.t_mtr_payment_channel")
|
|
.where("payment_channel_id_seq", id)
|
|
.update({
|
|
payment_channel_id_seq: payment_channel_id,
|
|
payment_channel_name_var: payment_channel_name,
|
|
logo_txt: logo,
|
|
active_boo: true,
|
|
updated_by_var: "",
|
|
});
|
|
|
|
if (result) {
|
|
return res.json({
|
|
code: "00",
|
|
message: "Data has been updated.",
|
|
data: result,
|
|
});
|
|
} else {
|
|
return res.json({
|
|
code: "01",
|
|
message: "No Data updated",
|
|
data: [],
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
// DELETE DATA BY ID
|
|
const deletePaymentChannel = async (req, res) => {
|
|
try {
|
|
let id = req.params.id;
|
|
|
|
let result = await knex("mtr.t_mtr_payment_channel")
|
|
.where("payment_channel_id_seq", id)
|
|
.del();
|
|
|
|
if (result) {
|
|
return res.json({
|
|
code: "00",
|
|
message: "Data has been deleted.",
|
|
data: result,
|
|
});
|
|
} else {
|
|
return res.json({
|
|
code: "01",
|
|
message: "No Data deleted",
|
|
data: [],
|
|
});
|
|
}
|
|
} catch (error) {
|
|
return res.json({
|
|
code: "01",
|
|
message: error.message,
|
|
data: [],
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
validate,
|
|
getPaymentChannels,
|
|
getPaymentChannelById,
|
|
createPaymentChannel,
|
|
updatePaymentChannel,
|
|
deletePaymentChannel,
|
|
};
|