diff --git a/controllers/AccountController.js b/controllers/AccountController.js index 089f657..6df0be9 100644 --- a/controllers/AccountController.js +++ b/controllers/AccountController.js @@ -30,11 +30,11 @@ class AccountController { const document = new Account({ name: name, account_type: account_type }); const account = await document.save(); - if(!account) { throw { code: 500, message: "FAILED_CREATE_ACCOUNT", data: null, status: false } } + if(!account) { throw { code: 500, message: "Gagal Menambahkan data akun!", data: null, status: false } } return res.status(200).json({ status: true, - message: "SUCCESS_CREATE_ACCOUNT", + message: "Berhasil menambahkan data akun!", data: account, }) } catch (error) { @@ -84,14 +84,12 @@ class AccountController { data: null }); - // check if account has transaction - const account = await Account.findByIdAndUpdate( { _id: id }, req.body, { new: true } ) - if(!account) { throw { code: 500, message: "ACCOUNT_UPDATE_FAILED", data: null, status: false } } + if(!account) { throw { code: 500, message: "Gagal mengubah data akun!", data: null, status: false } } return res.status(200).json({ status: true, - message: "ACCOUNT_UPDATE_SUCCESS", + message: "Berhasil mengubah data akun!", data: account, }) } catch (error) { @@ -116,14 +114,21 @@ class AccountController { }); // check if account has transaction - + const accounts = await Account.findById(id); + if(accounts.length > 0) { + return res.status(500).json({ + status: false, + message: "Gagal menghapus akun karena telah memiliki riwayat transaksi!", + data: null, + }); + } const account = await Account.findOneAndDelete({ _id: id }) - if(!account) { throw { code: 500, message: "ACCOUNT_DELETE_FAILED", data: null, status: false } } + if(!account) { throw { code: 500, message: "Gagal menghapus data akun!", data: null, status: false } } return res.status(200).json({ status: true, - message: "ACCOUNT_DELETE_SUCCESS", + message: "Berhasil menghapus data akun!", data: account, }) } catch (error) { diff --git a/controllers/AuthController.js b/controllers/AuthController.js index df969c4..d2a7aa4 100755 --- a/controllers/AuthController.js +++ b/controllers/AuthController.js @@ -26,16 +26,16 @@ class AuthController { async login(req, res) { try { const { email, password } = req.body - if(!email) { throw { code: 428, message: "EMAIL_IS_REQUIRED", data: null, status: false } } - if(!password) { throw { code: 428, message: "PASSWORD_IS_REQUIRED", data: null, status: false } } + if(!email) { throw { code: 428, message: "Email harap di isi!", data: null, status: false } } + if(!password) { throw { code: 428, message: "Password harap di isi!", data: null, status: false } } const user = await User.findOne({ email: email }) - if(!user) { throw { code: 403, message: "USER_NOT_FOUND", data: null, status: false } } + if(!user) { throw { code: 403, message: "Pengguna tidak ditemukan", data: null, status: false } } const isMatch = bcrypt.compareSync(password, user.password) - if(!isMatch) { throw { code: 403, message: "WRONG_PASSWORD", data: null, status: false } } + if(!isMatch) { throw { code: 403, message: "Password yang anda masukkan salah!", data: null, status: false } } - if(user.status === 'inactive') { throw { code: 403, message: "INACTIVE_USER", data: null, status: false } } + if(user.status === 'inactive') { throw { code: 403, message: "Pengguna tidak aktif!", data: null, status: false } } const payload = { id: user.id, role: user.role } const accessToken = await generateAccessToken(payload) @@ -46,7 +46,7 @@ class AuthController { return res.status(200).json({ status: true, - message: "LOGIN_SUCCESS", + message: "Berhasil Login", data: { user: data, accessToken, @@ -66,7 +66,7 @@ class AuthController { async refreshToken(req, res) { try { const { refreshToken } = req.body - if(!refreshToken) { throw { code: 428, message: "REFRESH_TOKEN_IS_REQUIRED", data: null, status: false } } + if(!refreshToken) { throw { code: 428, message: "Token diperlukan!", data: null, status: false } } const verify = jwt.verify(refreshToken, env.JWT_REFRESH_TOKEN_SECRET) const payload = { id: verify.id, role: verify.role } @@ -75,14 +75,14 @@ class AuthController { const _refreshToken = await generateRefreshToken(payload) const user = await User.findOne({ _id: verify.id }) - if(!user) { throw { code: 403, message: "USER_NOT_FOUND", data: null, status: false } } + if(!user) { throw { code: 403, message: "Pengguna tidak ditemukan", data: null, status: false } } let data = { ...user._doc } delete data.password return res.status(200).json({ status: true, - message: "REFRESH_TOKEN_SUCCESS", + message: "Berhasil menyegarkan token", data: { user: data, accessToken, @@ -93,9 +93,9 @@ class AuthController { if(!err.code) { err.code = 500 } if(err.message === "jwt expired") { - err.message = "REFRESH_TOKEN_EXPIRED" + err.message = "Token kadaluarsa" } else if(err.message === 'invalid signature' || err.message === 'invalid token') { - err.message = "REFRESH_TOKEN_INVALID" + err.message = "Token tidak valid" } return res.status(err.code).json({ diff --git a/controllers/TransactionController.js b/controllers/TransactionController.js index 85b7b0a..cae124b 100644 --- a/controllers/TransactionController.js +++ b/controllers/TransactionController.js @@ -8,7 +8,7 @@ class TransactionController { async index(req, res) { try { const query = await TransactionService.generateQuerySearch(req); - if(!query.status) throw { code: query.code, message: "ERROR_QUERY_SEARCH", data: null, status: false } + if(!query.status) throw { code: query.code, message: "Query Pencarian Eror!", data: null, status: false } let result = Transaction.aggregate(query.aggregate); const transactions = await result; diff --git a/controllers/TransactionLineController.js b/controllers/TransactionLineController.js index c70ad53..dbdfb1a 100644 --- a/controllers/TransactionLineController.js +++ b/controllers/TransactionLineController.js @@ -9,7 +9,7 @@ class TransactionController { async index(req, res) { try { const query = await TransactionLineService.generateQuerySearch(req); - if(!query.status) throw { code: query.code, message: "ERROR_QUERY_SEARCH", data: null, status: false } + if(!query.status) throw { code: query.code, message: "Query Pencarian Eror!", data: null, status: false } let result = TransactionLine.aggregate(query.aggregate); const lines = await result; diff --git a/controllers/UserController.js b/controllers/UserController.js index bf47bba..c7cef4d 100755 --- a/controllers/UserController.js +++ b/controllers/UserController.js @@ -11,7 +11,7 @@ class UserController { async index(req, res) { try { const query = await UserService.generateQuerySearch(req) - if(!query.status) throw { code: query.code, message: "ERROR_QUERY_SEARCH", data: null, status: false } + if(!query.status) throw { code: query.code, message: "Query Pencarian Eror!", data: null, status: false } const users = await User.aggregate([ { $match: query.query }, diff --git a/controllers/VendorController.js b/controllers/VendorController.js index 58e2d13..9c1e3ac 100644 --- a/controllers/VendorController.js +++ b/controllers/VendorController.js @@ -112,6 +112,16 @@ class VendorController { data: null }); + // check if account has transaction + const vendors = await Vendor.findById(id); + if(vendors.length > 0) { + return res.status(500).json({ + status: false, + message: "Gagal menghapus Vendor karena telah memiliki riwayat transaksi!", + data: null, + }); + } + const vendor = await Vendor.findOneAndDelete({ _id: id }) if(!vendor) { throw { code: 500, message: "VENDOR_DELETE_FAILED", data: null, status: false } } diff --git a/controllers/XlsxController.js b/controllers/XlsxController.js index fc202c8..d4f3677 100644 --- a/controllers/XlsxController.js +++ b/controllers/XlsxController.js @@ -1,9 +1,9 @@ import { writeFileSync } from "node:fs"; import { Workbook } from "xlsx-kaku"; -import Transaction from "../models/Transaction.js"; +import TransactionLine from "../models/TransactionLine.js"; -import TransactionService from '../services/TransactionService.js'; +import TransactionLineService from '../services/TransactionLineService.js'; import { fDate } from '../libraries/formatTime.js'; @@ -11,11 +11,11 @@ class XlsxController { async export(req, res) { try { console.log(req.query) - const query = await TransactionService.generateQuerySearch(req); + const query = await TransactionLineService.generateQuerySearch(req); if(!query.status) throw { code: query.code, message: "ERROR_QUERY_SEARCH", data: null, status: false } - let result = Transaction.aggregate(query.aggregate); + let result = TransactionLine.aggregate(query.aggregate); - const transactions = await result; + const lines = await result; const wb = new Workbook(); const ws = wb.addWorksheet("TRANSAKSI"); @@ -30,8 +30,8 @@ class XlsxController { let number = 0; let income = 0; let expense = 0; - transactions.map((transaction) => { - if(currentDate !== fDate(transaction.date)) { + lines.map((line) => { + if(currentDate !== fDate(line.date)) { if(row !== 3) { row += 1; ws.setCell(row, 0, { type: "string", value: "Total" }); @@ -43,9 +43,9 @@ class XlsxController { income = 0; expense = 0; row += 3; - currentDate = fDate(transaction.date); + currentDate = fDate(line.date); - ws.setCell(row, 0, { type: "string", value: `${fDate(transaction.date*1)}`, style: { alignment: {horizontal: "center", vertical: "center"} } }); + ws.setCell(row, 0, { type: "string", value: `${fDate(line.date*1)}`, style: { alignment: {horizontal: "center", vertical: "center"} } }); ws.setMergeCell({ ref: `A${row+1}:F${row+1}` }) row += 1 @@ -59,15 +59,15 @@ class XlsxController { number += 1; row += 1; - income += transaction.debit; - expense += transaction.credit; + income += line.debit; + expense += line.credit; ws.setCell(row, 0, { type: "number", value: number }); - ws.setCell(row, 1, { type: "string", value: transaction.label }); - ws.setCell(row, 2, { type: "string", value: transaction.accountId?.name || '' }); - ws.setCell(row, 3, { type: "string", value: transaction.vendorId?.name || '' }); - ws.setCell(row, 4, { type: "number", value: transaction.debit, style: { alignment: {horizontal: "right", vertical: "center"} } }); - ws.setCell(row, 5, { type: "number", value: transaction.credit, style: { alignment: {horizontal: "right", vertical: "center"} } }); + ws.setCell(row, 1, { type: "string", value: line.label }); + ws.setCell(row, 2, { type: "string", value: line.accountId?.name || '' }); + ws.setCell(row, 3, { type: "string", value: line.vendorId?.name || '' }); + ws.setCell(row, 4, { type: "number", value: line.debit, style: { alignment: {horizontal: "right", vertical: "center"} } }); + ws.setCell(row, 5, { type: "number", value: line.credit, style: { alignment: {horizontal: "right", vertical: "center"} } }); }) row += 1; diff --git a/middlewares/auth.js b/middlewares/auth.js index 6f7a45b..2c58907 100755 --- a/middlewares/auth.js +++ b/middlewares/auth.js @@ -12,9 +12,9 @@ const auth = () => { jwt.verify(token, env.JWT_ACCESS_TOKEN_SECRET, (err, data) => { if ( err ) { if( err.name == 'TokenExpiredError' ) { - throw 'TOKEN_EXPIRED' + throw 'Token Kadaluarsa!' } else { - throw 'TOKEN_IS_NOT_VALID' + throw 'Token tidak Valid!' } } else { req.jwt = data @@ -22,7 +22,7 @@ const auth = () => { } }) } else { - throw 'TOKEN_REQUIRED' + throw 'Token dibutuhkan!' } } catch (error) { return res.status(401).json({