Skip to content

Commit

Permalink
Merge pull request #143 from GiroudMathias/main
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip authored Mar 15, 2023
2 parents f1b9a78 + b5d0c08 commit 8c6fe5e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/server/services/account/account.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ export class AccountController {
}

@Export(ServerExports.AddBankBalance)
async addBankBalance(req: Request<{ amount: number; message: string }>, res: Response<unknown>) {
async addBankBalance(
req: Request<{ amount: number; message: string; fromIdentifier?: string }>,
res: Response<unknown>,
) {
try {
await this._accountService.addMoney(req);
res({ status: 'ok', data: {} });
Expand Down Expand Up @@ -300,7 +303,7 @@ export class AccountController {

@Export(ServerExports.RemoveBankBalance)
async removeBankBalance(
req: Request<{ amount: number; message: string }>,
req: Request<{ amount: number; message: string; toIdentifier?: string }>,
res: Response<unknown>,
) {
try {
Expand Down
55 changes: 49 additions & 6 deletions src/server/services/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ export class AccountService {

async addMoney(req: Request<UpdateBankBalanceInput>) {
logger.silly(`Adding money to ${req.source} ..`);
const { amount, message } = req.data;
const { amount, message, fromIdentifier } = req.data;

if (amount <= 0) {
throw new ServerError(GenericErrors.BadInput);
Expand All @@ -595,17 +595,27 @@ export class AccountService {
const t = await sequelize.transaction();

try {
let fromAccount = undefined;
const account = await this._accountDB.getDefaultAccountByIdentifier(user.getIdentifier());

if (!account) {
throw new ServerError(GenericErrors.NotFound);
}

if (fromIdentifier) {
logger.silly(`Adding money from ${fromIdentifier} ..`);
fromAccount = await this._accountDB.getDefaultAccountByIdentifier(fromIdentifier);
if (!fromAccount) {
throw new ServerError(GenericErrors.NotFound);
}
await this._accountDB.decrement(fromAccount, amount, t);
}
await this._accountDB.increment(account, amount, t);
await this._transactionService.handleCreateTransaction(
{
amount,
message,
fromAccount: fromAccount?.toJSON(),
toAccount: account?.toJSON(),
type: TransactionType.Incoming,
},
Expand All @@ -619,25 +629,36 @@ export class AccountService {

async addMoneyByIdentifier(req: Request<UpdateBankBalanceInput>) {
logger.silly(`Adding money by identifier to ${req.data.identifier} ..`);
const { amount, message, identifier } = req.data;
const { amount, message, identifier, fromIdentifier } = req.data;
if (amount <= 0) {
throw new ServerError(GenericErrors.BadInput);
}

const t = await sequelize.transaction();
try {
let fromAccount = undefined;
const account = await this._accountDB.getDefaultAccountByIdentifier(identifier ?? '');

if (!account) {
throw new ServerError(GenericErrors.NotFound);
}

if (fromIdentifier) {
logger.silly(`Adding money from ${fromIdentifier} ..`);
fromAccount = await this._accountDB.getDefaultAccountByIdentifier(fromIdentifier);
if (!fromAccount) {
throw new ServerError(GenericErrors.NotFound);
}
await this._accountDB.decrement(fromAccount, amount, t);
}

await this._accountDB.increment(account, amount, t);
await this._transactionService.handleCreateTransaction(
{
amount,
message,
fromAccount: account?.toJSON(),
fromAccount: fromAccount?.toJSON(),
toAccount: account?.toJSON(),
type: TransactionType.Incoming,
},
t,
Expand Down Expand Up @@ -668,7 +689,7 @@ export class AccountService {
{
amount,
message,
fromAccount: account?.toJSON(),
toAccount: account?.toJSON(),
type: TransactionType.Incoming,
},
t,
Expand All @@ -680,7 +701,7 @@ export class AccountService {
}

async removeMoney(req: Request<UpdateBankBalanceInput>) {
const { amount, message } = req.data;
const { amount, message, toIdentifier } = req.data;
logger.silly(`Removing ${amount} money from ${req.source}...`);

if (amount <= 0) {
Expand All @@ -691,11 +712,21 @@ export class AccountService {

const t = await sequelize.transaction();
try {
let toAccount = undefined;
const account = await this._accountDB.getDefaultAccountByIdentifier(user.getIdentifier());
if (!account) {
throw new ServerError(GenericErrors.NotFound);
}

if (toIdentifier) {
logger.silly(`Adding money to ${toIdentifier} ..`);
toAccount = await this._accountDB.getDefaultAccountByIdentifier(toIdentifier);
if (!toAccount) {
throw new ServerError(GenericErrors.NotFound);
}
await this._accountDB.decrement(toAccount, amount, t);
}

await account.update(
{
balance: account.getDataValue('balance') - amount,
Expand All @@ -708,6 +739,7 @@ export class AccountService {
amount: amount,
message: message,
fromAccount: account?.toJSON(),
toAccount: toAccount?.toJSON(),
type: TransactionType.Outgoing,
},
t,
Expand All @@ -719,7 +751,7 @@ export class AccountService {
}

async removeMoneyByIdentifier(req: Request<UpdateBankBalanceInput>) {
const { amount, identifier, message } = req.data;
const { amount, identifier, message, toIdentifier } = req.data;
logger.silly(`Removing ${amount} money by identifier from ${identifier} ..`);

if (amount <= 0) {
Expand All @@ -728,17 +760,28 @@ export class AccountService {

const t = await sequelize.transaction();
try {
let toAccount = undefined;
const account = await this._accountDB.getDefaultAccountByIdentifier(identifier ?? '');
if (!account) {
throw new ServerError(GenericErrors.NotFound);
}

if (toIdentifier) {
logger.silly(`Adding money to ${toIdentifier} ..`);
toAccount = await this._accountDB.getDefaultAccountByIdentifier(toIdentifier);
if (!toAccount) {
throw new ServerError(GenericErrors.NotFound);
}
await this._accountDB.decrement(toAccount, amount, t);
}

await this._accountDB.decrement(account, amount, t);
await this._transactionService.handleCreateTransaction(
{
amount,
message,
fromAccount: account?.toJSON(),
toAccount: toAccount?.toJSON(),
type: TransactionType.Outgoing,
},
t,
Expand Down
8 changes: 4 additions & 4 deletions src/server/services/cash/cash.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ export class CashController {
}

@Export(ServerExports.AddCash)
async addCash(req: Request<ChangeCashInput>, res: Response<boolean>) {
async addCash(req: Request<number>, res: Response<boolean>) {
try {
await this._cashService.handleAddCash(req.data.source, req.data.amount);
await this._cashService.handleAddCash(req.source, req.data);
res({ status: 'ok', data: true });
} catch (error) {
res({ status: 'error', errorMsg: error.message });
}
}

@Export(ServerExports.RemoveCash)
async removeCash(req: Request<ChangeCashInput>, res: Response<boolean>) {
async removeCash(req: Request<number>, res: Response<boolean>) {
try {
await this._cashService.handleRemoveCash(req.data.source, req.data.amount);
await this._cashService.handleRemoveCash(req.source, req.data);
res({ status: 'ok', data: true });
} catch (error) {
res({ status: 'error', errorMsg: error.message });
Expand Down
6 changes: 5 additions & 1 deletion src/server/services/invoice/invoice.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class InvoiceDB {
identifier: string,
pagination: GetInvoicesInput,
): Promise<InvoiceModel[]> {
return await InvoiceModel.findAll({ where: { toIdentifier: identifier }, ...pagination });
return await InvoiceModel.findAll({
where: { toIdentifier: identifier },
...pagination,
order: [['createdAt', 'DESC']],
});
}

async getReceivedInvoicesCount(identifier: string): Promise<number> {
Expand Down

0 comments on commit 8c6fe5e

Please sign in to comment.