-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into IOBP-651-adapt-new-wallet-attributes
- Loading branch information
Showing
24 changed files
with
340 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import * as O from "fp-ts/lib/Option"; | ||
import { TransactionListItem } from "../../../../generated/definitions/pagopa/transactions/TransactionListItem"; | ||
import { TransactionDetailResponse } from "../../../../generated/definitions/pagopa/transactions/TransactionDetailResponse"; | ||
import { TransactionInfo } from "../../../../generated/definitions/pagopa/ecommerce/TransactionInfo"; | ||
import { generateRandomInfoTransaction } from "../utils/transactions"; | ||
import { ioDevServerConfig } from "../../../config"; | ||
|
||
const mockedTaxCodes = ["1199250158", "13756881002", "262700362", "31500945"]; | ||
|
||
type TransactionId = TransactionListItem["transactionId"]; | ||
|
||
const userTransactions = new Map<TransactionId, TransactionListItem>(); | ||
const transactions = new Map<TransactionId, TransactionDetailResponse>(); | ||
|
||
const getUserTransactions = () => | ||
Array.from(userTransactions.size > 0 ? userTransactions.values() : []); | ||
|
||
const getTransactionDetails = (transactionId: TransactionId) => | ||
pipe( | ||
transactions, | ||
O.fromNullable, | ||
O.chain(transactions => O.fromNullable(transactions.get(transactionId))) | ||
); | ||
|
||
const addUserTransaction = (transaction: TransactionListItem) => { | ||
userTransactions.set(transaction.transactionId, transaction); | ||
}; | ||
|
||
const removeUserTransaction = (transactionId: TransactionId) => { | ||
userTransactions.delete(transactionId); | ||
removeTransactionDetails(transactionId); | ||
}; | ||
|
||
const addTransactionDetails = ( | ||
transactionId: TransactionId, | ||
transaction: TransactionDetailResponse | ||
) => { | ||
transactions.set(transactionId, transaction); | ||
}; | ||
|
||
const removeTransactionDetails = (transactionId: TransactionId) => { | ||
transactions.delete(transactionId); | ||
}; | ||
|
||
const generateUserTransaction = ( | ||
transactionId: TransactionId, | ||
idx: number, | ||
additionalTransactionInfo: Partial<TransactionInfo> = {} | ||
) => { | ||
const payeeTaxCode = | ||
mockedTaxCodes[ | ||
faker.datatype.number({ min: 0, max: mockedTaxCodes.length - 1 }) | ||
]; | ||
const randomTransaction: TransactionListItem = { | ||
transactionId, | ||
payeeName: faker.company.name(), | ||
payeeTaxCode, | ||
amount: additionalTransactionInfo.payments?.[0]?.amount.toString(), | ||
transactionDate: new Date( | ||
new Date().setDate(new Date().getDate() - 2 * idx) | ||
).toISOString(), | ||
isCart: false | ||
}; | ||
|
||
const cartList = Array.from( | ||
{ length: faker.datatype.number({ min: 1, max: 2 }) }, | ||
() => ({ | ||
subject: faker.lorem.sentence(faker.datatype.number({ min: 2, max: 4 })), | ||
amount: faker.finance.amount(1, 1000), | ||
payee: { | ||
name: randomTransaction.payeeName, | ||
taxCode: randomTransaction.payeeTaxCode | ||
}, | ||
debtor: { | ||
name: faker.name.fullName(), | ||
taxCode: faker.random.alphaNumeric(16).toUpperCase() | ||
}, | ||
refNumberType: "IBAN", | ||
refNumberValue: faker.datatype | ||
.number({ min: 100000000000, max: 999999999999 }) | ||
.toString() | ||
}) | ||
); | ||
// eslint-disable-next-line functional/immutable-data | ||
randomTransaction.isCart = cartList.length > 1; | ||
// eslint-disable-next-line functional/immutable-data | ||
randomTransaction.amount = cartList | ||
.reduce((acc, item) => acc + Number(item.amount), 0) | ||
.toString(); | ||
addUserTransaction(randomTransaction); | ||
|
||
const randomTransactionDetails: TransactionDetailResponse = { | ||
infoTransaction: generateRandomInfoTransaction(cartList), | ||
carts: cartList | ||
}; | ||
addTransactionDetails(transactionId, randomTransactionDetails); | ||
return randomTransaction; | ||
}; | ||
|
||
const generateUserTransactionData = () => { | ||
for ( | ||
// eslint-disable-next-line functional/no-let | ||
let i = 0; | ||
i < ioDevServerConfig.features.payments.numberOfTransactions; | ||
i = i + 1 | ||
) { | ||
generateUserTransaction(faker.datatype.uuid(), i); | ||
} | ||
}; | ||
|
||
// At server startup | ||
generateUserTransactionData(); | ||
|
||
export default { | ||
addUserTransaction, | ||
getUserTransactions, | ||
getTransactionDetails, | ||
generateUserTransaction, | ||
removeUserTransaction | ||
}; |
File renamed without changes.
1 change: 1 addition & 0 deletions
1
src/features/wallet/routers/index.ts → src/features/payments/routers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import "./payment"; | ||
import "./wallets"; | ||
import "./transactions"; | ||
|
||
export { walletRouter } from "./router"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as O from "fp-ts/lib/Option"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import TransactionsDB from "../persistence/transactions"; | ||
|
||
import { TransactionListWrapResponse } from "../../../../generated/definitions/pagopa/transactions/TransactionListWrapResponse"; | ||
import { addTransactionHandler } from "./router"; | ||
|
||
const CONTINUATION_TOKEN_HEADER = "x-continuation-token"; | ||
|
||
addTransactionHandler("get", "/transactions", (req, res) => { | ||
const size = req.query.size ? Number(req.query.size) : 10; | ||
const offset = | ||
req.headers[CONTINUATION_TOKEN_HEADER] !== undefined && | ||
req.headers[CONTINUATION_TOKEN_HEADER] !== "undefined" | ||
? Number(req.headers[CONTINUATION_TOKEN_HEADER]) | ||
: 0; | ||
const response: TransactionListWrapResponse = { | ||
transactions: TransactionsDB.getUserTransactions().slice( | ||
offset, | ||
offset + size | ||
) | ||
}; | ||
const continuationToken = | ||
TransactionsDB.getUserTransactions().length > offset + size | ||
? (offset + size).toString() | ||
: undefined; | ||
pipe( | ||
response.transactions, | ||
O.fromNullable, | ||
O.chain(O.fromPredicate(transactions => transactions.length > 0)), | ||
O.fold( | ||
() => | ||
res.status(404).json({ | ||
title: "No transactions found", | ||
status: 404, | ||
detail: "No transactions found for the user" | ||
}), | ||
_ => { | ||
if (continuationToken) { | ||
res.setHeader(CONTINUATION_TOKEN_HEADER, continuationToken); | ||
} | ||
return res.status(200).json(response); | ||
} | ||
) | ||
); | ||
}); | ||
|
||
addTransactionHandler("get", "/transactions/:transactionId", (req, res) => { | ||
pipe( | ||
req.params.transactionId, | ||
O.fromNullable, | ||
O.fold( | ||
() => res.sendStatus(400), | ||
transactionId => { | ||
const transaction = TransactionsDB.getTransactionDetails(transactionId); | ||
return pipe( | ||
transaction, | ||
O.fold( | ||
() => res.sendStatus(404), | ||
transaction => res.status(200).json(transaction) | ||
) | ||
); | ||
} | ||
) | ||
); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.