const { body, validationResult } = require('express-validator'); const helper = require('../../helpers/helper'); const winston = require('../../helpers/winston.logger'); const { generateInvoice: _generateInvoice, retryInvoices: _postingInvoice } = require('./invoice-posting.service'); const moment = require('moment'); moment.locale('id'); // VALIDATION const validate = (method) => { switch (method) { case 'generateInvoice': return [ body('trx_date') .notEmpty() .withMessage('trx_date is required!') .isISO8601() .withMessage('invalid trx_date format YYYY-MM-DD !') .escape() .trim(), body('repost') .notEmpty() .withMessage('repost is required!') .isBoolean() .withMessage('repost must boolean!') .escape() .trim(), ]; case 'postingInvoice': return [ body('trx_date') .notEmpty() .withMessage('trx_date is required!') .isISO8601() .withMessage('invalid trx_date format YYYY-MM-DD !') .escape() .trim(), ]; default: break; } }; // POSTING INVOICE const apiGenerateInvoice = async (req, res) => { try { // generate unique code const uniqueCode = helper.getUniqueCode(); let result = {}; // log info winston.logger.info( `${uniqueCode} REQUEST generate invoice` ); const schedulerCode = 'GENERATEINVOICEONLINETOPUP'; const startData = await helper.startScheduler(schedulerCode); const token = startData.c_execution_token ?? null; // check validator const err = validationResult(req, res); if (!err.isEmpty()) { result = { code: '400', message: err.errors[0].msg, data: {}, }; // log warn winston.logger.warn( `${uniqueCode} RESPONSE generate invoice code=${result.code}` ); return res.status(200).json(result); } // time const invoiceDate = moment(req.body.trx_date).format('YYYY-MM-DD'); const invoice = await _generateInvoice( invoiceDate, uniqueCode, req.body.repost ); result = { code: '200', message: 'Succes generate invoice online topup', data: invoice, }; await helper.finishScheduler(token, schedulerCode); return res.status(200).json(result); } catch (error) { // create log winston.logger.error( `500 internal server error - backend server | ${error.message}` ); return res.status(200).json({ code: '500', message: process.env.NODE_ENV != 'production' ? error.message : '500 internal server error - backend server.', data: {}, }); } }; // RETRY POSTING INVOICE const apiPostingInvoice = async (req, res) => { try { // generate unique code const uniqueCode = helper.getUniqueCode(); let result = {}; // log info winston.logger.info( `${uniqueCode} REQUEST posting invoice` ); const schedulerCode = 'POSTINGINVOICEONLINETOPUP'; const startData = await helper.startScheduler(schedulerCode); const token = startData.c_execution_token ?? null; // check validator const err = validationResult(req, res); if (!err.isEmpty()) { result = { code: '400', message: err.errors[0].msg, data: {}, }; // log warn winston.logger.warn( `${uniqueCode} RESPONSE generate invoice code=${result.code}` ); return res.status(200).json(result); } // return uniqueCode winston.logger.info(`SCHEDULER-RETRY-${uniqueCode} RE-TRY POSTING INVOICE`); const invoice = await _postingInvoice(uniqueCode); result = { code: '200', message: 'Succes', data: invoice, }; await helper.finishScheduler(token, schedulerCode); return res.status(200).json(result); } catch (error) { // create log winston.logger.error( `500 internal server error - backend server | ${error.message}` ); return res.status(200).json({ code: '500', message: process.env.NODE_ENV != 'production' ? error.message : '500 internal server error - backend server.', data: {}, }); } }; module.exports = { validate, apiPostingInvoice, apiGenerateInvoice, _postingInvoice, _generateInvoice, };