Skip to content

Commit

Permalink
Merge pull request #318 from pagopa/IOBP-347-idpay-barcode-generation…
Browse files Browse the repository at this point in the history
…-dev-server

[IOBP-347] addition of IDPAY barcode
  • Loading branch information
forrest57 authored Nov 14, 2023
2 parents 284edc2 + cea6f0d commit 4f7bdf1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/persistence/idpay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import { WalletV2 } from "../../generated/definitions/pagopa/WalletV2";
import { ioDevServerConfig } from "../config";
import { getRandomEnumValue } from "../payloads/utils/random";
import { getWalletV2 } from "../routers/walletsV2";
import {
StatusEnum,
TransactionBarCodeResponse
} from "../../generated/definitions/idpay/TransactionBarCodeResponse";

const idPayConfig = ioDevServerConfig.features.idpay;
const { idPay: walletConfig } = ioDevServerConfig.wallet;
Expand Down Expand Up @@ -526,3 +530,40 @@ export const enrollCodeToInitiative = (initiativeId: string): boolean => {

return true;
};

// eslint-disable-next-line functional/no-let
let barcodeTransactions: {
[initiativeID: string]: TransactionBarCodeResponse;
} = {};

export const getIdPayBarcodeTransaction = (
initiativeId: string,
trxExpirationMinutes: number = 1
): TransactionBarCodeResponse => {
const currentBarcode = barcodeTransactions[initiativeId];
if (currentBarcode === undefined) {
const newBarcodeTransaction: TransactionBarCodeResponse = {
id: ulid(),
trxCode: faker.random.alphaNumeric(8),
initiativeId,
status: StatusEnum.CREATED,
trxDate: new Date(),
trxExpirationMinutes
};
barcodeTransactions = {
...barcodeTransactions,
[initiativeId]: newBarcodeTransaction
};
return newBarcodeTransaction;
} else {
const timeDiff = new Date().getTime() - currentBarcode.trxDate.getTime();
// trxExpirationMinutes is in minutes, timeDiff is in milliseconds
const isExpired = timeDiff > trxExpirationMinutes * 60 * 1000;
if (isExpired) {
// eslint-disable-next-line functional/immutable-data
delete barcodeTransactions[initiativeId];
return getIdPayBarcodeTransaction(initiativeId, trxExpirationMinutes);
}
return currentBarcode;
}
};
33 changes: 33 additions & 0 deletions src/routers/features/idpay/barcode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as O from "fp-ts/lib/Option";

import { pipe } from "fp-ts/lib/function";
import { TransactionBarCodeRequest } from "../../../../generated/definitions/idpay/TransactionBarCodeRequest";
import { getIdPayError } from "../../../payloads/features/idpay/error";
import { getWalletDetailResponse } from "../../../payloads/features/idpay/get-wallet-detail";
import { getIdPayBarcodeTransaction } from "../../../persistence/idpay";
import { addIdPayHandler } from "./router";

addIdPayHandler("post", "/payment/bar-code", (req, res) =>
pipe(
TransactionBarCodeRequest.decode(req.body),
O.fromEither,
O.map(data => data.initiativeId),
O.fold(
() => res.status(400).json(getIdPayError(400)),
initiativeId =>
pipe(
getWalletDetailResponse(initiativeId),
O.fold(
() => res.status(404).json(getIdPayError(404)),
() => {
const barcodeTransaction = getIdPayBarcodeTransaction(
initiativeId,
2
);
return res.status(201).json(barcodeTransaction);
}
)
)
)
)
);
1 change: 1 addition & 0 deletions src/routers/features/idpay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import "./iban";
import "./wallet";
import "./timeline";
import "./payment";
import "./barcode";

export { idpayRouter } from "./router";

0 comments on commit 4f7bdf1

Please sign in to comment.