-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransfer-service.ts
167 lines (152 loc) · 6.1 KB
/
transfer-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* SudoSOS back-end API service.
* Copyright (C) 2020 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import dinero, { Dinero } from 'dinero.js';
import {
EntityManager,
FindManyOptions,
} from 'typeorm';
import Transfer from '../entity/transactions/transfer';
import { PaginatedTransferResponse, TransferResponse } from '../controller/response/transfer-response';
import TransferRequest from '../controller/request/transfer-request';
import User from '../entity/user/user';
import QueryFilter, { FilterMapping } from '../helpers/query-filter';
import { PaginationParameters } from '../helpers/pagination';
import { RequestWithToken } from '../middleware/token-middleware';
import { asNumber } from '../helpers/validators';
import { parseUserToBaseResponse } from '../helpers/revision-to-response';
import InvoiceService from './invoice-service';
import StripeService from './stripe-service';
import PayoutRequestService from './payout-request-service';
import DebtorService from './debtor-service';
export interface TransferFilterParameters {
id?: number;
createdById?: number,
fromId?: number,
toId?: number
}
export function parseGetTransferFilters(req: RequestWithToken): TransferFilterParameters {
const filters: TransferFilterParameters = {
id: asNumber(req.query.id),
createdById: asNumber(req.query.id),
fromId: asNumber(req.query.id),
toId: asNumber(req.query.id),
};
return filters;
}
export default class TransferService {
public static asTransferResponse(transfer: Transfer) : TransferResponse {
return {
amount: transfer.amount.toObject(),
from: parseUserToBaseResponse(transfer.from, false),
to: parseUserToBaseResponse(transfer.to, false),
id: transfer.id,
description: transfer.description,
createdAt: transfer.createdAt.toISOString(),
updatedAt: transfer.updatedAt.toISOString(),
invoice: transfer.invoice ? InvoiceService.asInvoiceResponse(transfer.invoice) : null,
deposit: transfer.deposit ? StripeService.asStripeDepositResponse(transfer.deposit) : null,
payoutRequest: transfer.payoutRequest ? PayoutRequestService.asBasePayoutRequestResponse(transfer.payoutRequest) : null,
fine: transfer.fine ? DebtorService.asFineResponse(transfer.fine) : null,
waivedFines: transfer.waivedFines ? DebtorService.asUserFineGroupResponse(transfer.waivedFines) : null,
};
}
public static async createTransfer(request: TransferRequest, manager?: EntityManager) : Promise<Transfer> {
const transfer = Object.assign(new Transfer(), {
description: request.description,
amount: dinero(request.amount as Dinero.Options),
from: request.fromId ? await User.findOne({ where: { id: request.fromId } }) : undefined,
to: request.toId ? await User.findOne({ where: { id: request.toId } }) : undefined,
});
if (manager) {
await manager.save(transfer);
} else {
await transfer.save();
}
return transfer;
}
/**
* Query to return transfers from the database
* @param filters - Parameters to query the transfers with
* @param pagination
* @param user
*/
public static async getTransfers(filters: TransferFilterParameters = {},
pagination: PaginationParameters = {}, user?: User)
: Promise<PaginatedTransferResponse> {
const { take, skip } = pagination;
const filterMapping: FilterMapping = {
id: 'id',
fromId: 'fromId',
toId: 'toId',
type: 'type',
};
const whereClause = QueryFilter.createFilterWhereClause(filterMapping, filters);
let whereOptions: any = [];
// Apparently this is how you make a and-or clause in typeorm without a query builder.
if (user) {
whereOptions = [{
fromId: user.id,
...whereClause,
}, {
toId: user.id,
...whereClause,
}];
} else {
whereOptions = whereClause;
}
const options: FindManyOptions = {
where: whereOptions,
relations: ['from', 'to',
'invoice', 'invoice.invoiceStatus',
'deposit', 'deposit.depositStatus',
'payoutRequest', 'payoutRequest.payoutRequestStatus', 'payoutRequest.requestedBy',
'fine', 'fine.userFineGroup', 'fine.userFineGroup.user',
'waivedFines', 'waivedFines.fines', 'waivedFines.fines.userFineGroup',
],
take,
skip,
order: { createdAt: 'DESC' },
};
const results = await Promise.all([
Transfer.find(options),
Transfer.count(options),
]);
const records = results[0].map((rawTransfer) => this.asTransferResponse(rawTransfer));
return {
_pagination: {
take, skip, count: results[1],
},
records,
};
}
public static async postTransfer(request: TransferRequest) : Promise<TransferResponse> {
const transfer = await this.createTransfer(request);
return this.asTransferResponse(transfer);
}
public static async verifyTransferRequest(request: TransferRequest) : Promise<boolean> {
// the type of the request should be in TransferType enums
// if the type is custom a description is necessary
// a transfer is always at least from a valid user OR to a valid user
// a transfer may be from null to an user, or from an user to null
return (request.fromId || request.toId)
&& (await User.findOne({ where: { id: request.fromId } })
|| await User.findOne({ where: { id: request.toId } }))
&& request.amount.precision === dinero.defaultPrecision
&& request.amount.currency === dinero.defaultCurrency;
}
}