Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create schedule #11

Merged
merged 9 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/.env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
SECRET=ToMakeNewTokenForNewUser
SECRET=ToMakeNewTokenForNewUser
SECRET_KEY_XENDIT=xnd_development_S1YRENVA6FA1ELZerCvKR3NaBY1J7Jm2JLLhawkAzF7R5gTg4SvcAZfaXPIKA0n
API_KEY_DAILY_CO=29609d3d8928f72e9c0d09e17160a43f85642e2f7a651e540f35a9395c5517c8
10 changes: 10 additions & 0 deletions server/coba.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const tanggal = new Date();
console.log(tanggal);
console.log(tanggal.getTime(), "milisecond");
console.log(tanggal.getDay(), "day");
console.log(tanggal.getHours(), "hour");
console.log(tanggal.getMinutes(), "minutes");
console.log(tanggal.toLocaleString("id-ID"), "Indo");
console.log(tanggal.toUTCString(), "pusat");
console.log(tanggal.getUTCHours(), "pusat");
console.log(5 * 60 * 1000);
132 changes: 85 additions & 47 deletions server/controllers/loanController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
// const {} = require("")
const {Loan, Borrower} = require("../models/index");
const { XenditInvoice, XenditDisbursement } = require('../helpers/Xendit');

class LoanController {
static async CreateInvoice(req, res, next) {
const { email } = req.body;
const loanID = '123456';
static async CreateInvoiceLender(req, res, next) {
// const { userID, email } = req.user.email; //tunggu auth
const lenderID = 1
const email = "dharmasatrya10@gmail.com"
const {amount, tenor} = req.body
const randomID = Math.random().toString(36).slice(2)
try {
const loanData = {
externalID: randomID,
lenderID: lenderID,
borrowerID: null,
status: "pending",
initialLoan: amount,
tenor: tenor
}

const createLoan = await Loan.create(loanData)

const invoice = await XenditInvoice.createInvoice({
externalID: 'EXTERNAL_ID_KITA',
payerEmail: 'pinjamkuproject@gmail.com',
description: `Invoice for loan ${loanID}`,
amount: 100000,
externalID: `invoice-lender-${randomID}`,
payerEmail: email,
description: `Invoice for loan ${randomID}`,
amount: amount,
shouldSendEmail: true
});
res.status(200).json({
Expand All @@ -22,16 +36,33 @@ class LoanController {
}
}

// static async CreateLoan(req, res, next) {
// const {} = req.body;
// try {
// } catch (error) {
// next(error);
// }
// }
static async CreateInvoiceBorrower(req, res, next) {
// const {email} = req.user.email
const email = "dharmasatrya10@gmail.com"
const { loanID } = req.body;
try {
const loanData = await Loan.findOne({where: {id: loanID}})

const amountWithInterest = loanData.initialLoan + loanData.initialLoan * 0.07

const invoice = await XenditInvoice.createInvoice({
externalID: `invoice-borrower-${loanData.externalID}`,
payerEmail: email,
description: `Invoice for loan ${loanData.externalID}`,
amount: amountWithInterest,
shouldSendEmail: true
});
res.status(200).json({
externalID: invoice.external_id,
invoiceURL: invoice.invoice_url
});
} catch (error) {
console.log(error);
}
}

static async CreateWithdrawal(req, res, next) {
const {} = req.body;
static async CreateWithdrawal(req, res, next) { //disburse ke lender
const {lenderID} = req.body;

try {
const loan = {
Expand Down Expand Up @@ -60,42 +91,49 @@ class LoanController {
}
}

static async CreateDisbursement(req, res, next) {
const {} = req.body;
static async CreateDisbursement(req, res, next) { //disburse ke borrower
const {loanID} = req.body;
// const borrowerID = req.userID
const borrowerID = 1
try {
const loan = {
id: '123',
initialLoan: 100000,
borrower: {
bankCode: 'BCA',
accountHolderName: 'Test',
accountNumber: '1234567890'
}
const loanData = await Loan.findOne({where: {id:loanID}})
const borrowerData = await Borrower.findOne({where: {id: borrowerID}})

const amountWithInterest = loanData.initialLoan + loanData.initialLoan * 0.07

const loanDataInput = {
externalID: `disburse-borrower-${loanData.externalID}`,
bankCode: borrowerData.bankCode,
accountHolderName: borrowerData.holderName,
accountNumber: `${borrowerData.accountNumber}`,
description: `loan disbursement for ${loanData.externalID}`,
amount: amountWithInterest
};

const withdrawalInterest = loan.initialLoan + loan.initialLoan * 0.05;
console.log(loanDataInput)

const disbursement = await XenditDisbursement.create({
externalID: `disburse-${loan.id}`,
bankCode: loan.lender.bankCode,
accountHolderName: loan.lender.accountHolderName,
accountNumber: loan.lender.accountNumber,
description: `withdrawal for ${loan.id}`,
amount: withdrawalInterest
});
const disbursement = await XenditDisbursement.create(loanDataInput);
res.status(200).json(disbursement);
} catch (error) {
console.log(error);
}
}

static async InvoiceEndpoint(req, res, next) {
const { external_id, status } = req.body;
const { external_id, status, amount } = req.body;
try {
if (status === 'FAILED') {
res.status(200).json({ status: 'failed' });
} else if (status === 'PAID') {
res.status(200).json({ status: 'paid' });
if(external_id.includes("borrower")){
const loanID = external_id.split("-")[2]
const loanData = await Loan.update({status: "complete"}, {where: {externalID: loanID}})
res.status(200).json({ status: 'complete', id: loanID });
} else {
const loanID = external_id.split("-")[2]
const loanData = await Loan.update({ status: "active" }, {where: {externalID: loanID}, returning: true})
res.status(200).json({ status: 'active', id: loanID });
}
}
} catch (error) {
console.log(error);
Expand All @@ -108,20 +146,20 @@ class LoanController {
if (status === 'FAILED') {
res.status(200).json({ status: 'failed' });
} else if (status === 'COMPLETED') {
res.status(200).json({ status: 'paid' });
if(external_id.includes("borrower")){
const loanID = external_id.split("-")[2]
const loanData = await Loan.update({status: "borrowed"}, {where: {externalID: loanID}})
res.status(200).json({ status: 'borrower', id: loanID });
} else {
const loanID = external_id.split("-")[2]
const loanData = await Loan.update({status: "withdrawn"}, {where: {externalID: loanID}})
res.status(200).json({ status: 'lender', id: loanID });
}
}
} catch (error) {
console.log(error);
}
}

static Test(req, res, next) {
try {
res.status(200).json({ msg: 'ok' });
} catch (error) {
res.status(500);
}
}
}

module.exports = LoanController;
Loading