-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitemToTransaction.ts
More file actions
61 lines (50 loc) · 1.94 KB
/
itemToTransaction.ts
File metadata and controls
61 lines (50 loc) · 1.94 KB
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
/**
* @author Labs64 <netlicensing@labs64.com>
* @license Apache-2.0
* @link https://netlicensing.io
* @copyright 2017 Labs64 NetLicensing
*/
import itemToObject from '@/converters/itemToObject';
// entities
import License from '@/entities/License';
import LicenseTransactionJoin from '@/entities/LicenseTransactionJoin';
import Transaction from '@/entities/Transaction';
// types
import { Item } from '@/types/api/response';
import { TransactionProps } from '@/types/entities/Transaction';
export default <T extends object = TransactionProps>(item?: Item) => {
const props = itemToObject<Record<string, unknown>>(item, {
active: 'boolean',
number: 'string',
status: 'string',
source: 'string',
grandTotal: 'number',
discount: 'number',
currency: 'string',
datecreated: 'string',
dateclosed: 'string',
paymentMethod: 'string',
licenseTransactionJoins: 'json',
inUse: 'boolean',
});
const { datecreated: dateCreated, dateclosed: dateClosed } = props;
if (dateCreated && typeof dateCreated === 'string') {
props.dateCreated = new Date(dateCreated);
delete props.datecreated;
}
if (dateClosed && typeof dateClosed === 'string') {
props.dateClosed = new Date(dateClosed);
delete props.dateclosed;
}
type LicenseTransactionJoins = { licenseNumber: string | number; transactionNumber: string | number }[] | undefined;
const licenseTransactionJoins: LicenseTransactionJoins = props.licenseTransactionJoin as LicenseTransactionJoins;
delete props.licenseTransactionJoin;
if (licenseTransactionJoins) {
props.licenseTransactionJoins = licenseTransactionJoins.map(({ transactionNumber, licenseNumber }) => {
const transaction = Transaction({ number: String(transactionNumber) });
const license = License({ number: String(licenseNumber) });
return LicenseTransactionJoin(transaction, license);
});
}
return Transaction<T>(props as TransactionProps<T>);
};