Skip to content

Commit

Permalink
repair return message
Browse files Browse the repository at this point in the history
  • Loading branch information
zdni committed Aug 1, 2024
1 parent 6f06567 commit 4a98464
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 42 deletions.
23 changes: 14 additions & 9 deletions controllers/AccountController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
22 changes: 11 additions & 11 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -46,7 +46,7 @@ class AuthController {

return res.status(200).json({
status: true,
message: "LOGIN_SUCCESS",
message: "Berhasil Login",
data: {
user: data,
accessToken,
Expand All @@ -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 }
Expand All @@ -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,
Expand All @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion controllers/TransactionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion controllers/TransactionLineController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
10 changes: 10 additions & 0 deletions controllers/VendorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }

Expand Down
32 changes: 16 additions & 16 deletions controllers/XlsxController.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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';

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");

Expand All @@ -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" });
Expand All @@ -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
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ 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
next()
}
})
} else {
throw 'TOKEN_REQUIRED'
throw 'Token dibutuhkan!'
}
} catch (error) {
return res.status(401).json({
Expand Down

0 comments on commit 4a98464

Please sign in to comment.