Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [IOBP-927] Adapt new openAPI biz-events #423

Merged
merged 10 commits into from
Oct 17, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"api_trial_system": "https://raw.githubusercontent.com/pagopa/io-backend/v14.3.0-RELEASE/api_trial_system.yaml",
"api_pagopa_walletv3": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.202.0/src/domains/pay-wallet-app/api/io-payment-wallet/v1/_openapi.json.tpl",
"api_pagopa_ecommerce": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.202.0/src/domains/ecommerce-app/api/ecommerce-io/v2/_openapi.json.tpl",
"api_pagopa_biz_events": "https://raw.githubusercontent.com/pagopa/pagopa-biz-events-service/0.1.37/openapi/openapi_io_patch.json",
"api_pagopa_biz_events": "https://raw.githubusercontent.com/pagopa/pagopa-biz-events-service/0.1.57/openapi/openapi_io_patch_lap.json",
"api_pagopa_platform": "https://raw.githubusercontent.com/pagopa/pagopa-infra/v1.64.0/src/domains/shared-app/api/session-wallet/v1/_openapi.json.tpl",
"api_services": "https://raw.githubusercontent.com/pagopa/io-backend/master/api_services_app_backend.yaml",
"author": "Matteo Boschi",
Expand Down
121 changes: 121 additions & 0 deletions src/features/payments/persistence/notices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { faker } from "@faker-js/faker";
import { pipe } from "fp-ts/lib/function";
import * as O from "fp-ts/lib/Option";
import { NoticeListItem } from "../../../../generated/definitions/pagopa/transactions/NoticeListItem";
import { NoticeDetailResponse } from "../../../../generated/definitions/pagopa/transactions/NoticeDetailResponse";
import { TransactionInfo } from "../../../../generated/definitions/pagopa/ecommerce/TransactionInfo";
import { generateRandomInfoNotice } from "../utils/transactions";
import { ioDevServerConfig } from "../../../config";

const mockedTaxCodes = ["1199250158", "13756881002", "262700362", "31500945"];

type EventId = NoticeListItem["eventId"];

const userNotices = new Map<EventId, NoticeListItem>();
const notices = new Map<EventId, NoticeDetailResponse>();

const getUserNotices = () =>
Array.from(userNotices.size > 0 ? userNotices.values() : []);

const getNoticeDetails = (eventId: EventId) =>
pipe(
notices,
O.fromNullable,
O.chain(notices => O.fromNullable(notices.get(eventId)))
);

const addUserNotice = (transaction: NoticeListItem) => {
userNotices.set(transaction.eventId, transaction);
};

const removeUserNotice = (eventId: EventId) => {
userNotices.delete(eventId);
removeNoticeDetails(eventId);
};

const addNoticeDetails = (eventId: EventId, notice: NoticeDetailResponse) => {
notices.set(eventId, notice);
};

const removeNoticeDetails = (eventId: EventId) => {
notices.delete(eventId);
};

const generateUserNotice = (
eventId: EventId,
idx: number,
additionalTransactionInfo: Partial<TransactionInfo> = {}
) => {
const payeeTaxCode =
mockedTaxCodes[
faker.datatype.number({ min: 0, max: mockedTaxCodes.length - 1 })
];
const randomNotice: NoticeListItem = {
eventId,
payeeName: faker.company.name(),
payeeTaxCode,
isDebtor: false,
isPayer: true,
amount: additionalTransactionInfo.payments?.[0]?.amount.toString() || "",
noticeDate: 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: randomNotice.payeeName,
taxCode: randomNotice.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
LeleDallas marked this conversation as resolved.
Show resolved Hide resolved
randomNotice.isCart = cartList.length > 1;
// eslint-disable-next-line functional/immutable-data
randomNotice.amount = cartList
.reduce((acc, item) => acc + Number(item.amount), 0)
.toString();
addUserNotice(randomNotice);

const randomNoticeDetails: NoticeDetailResponse = {
infoNotice: generateRandomInfoNotice(cartList),
carts: cartList
};
addNoticeDetails(eventId, randomNoticeDetails);
return randomNotice;
};

const generateUserNoticeData = () => {
for (
// eslint-disable-next-line functional/no-let
let i = 0;
i < ioDevServerConfig.features.payments.numberOfTransactions;
i = i + 1
) {
generateUserNotice(faker.datatype.uuid(), i);
}
Hantex9 marked this conversation as resolved.
Show resolved Hide resolved
};

// At server startup
generateUserNoticeData();

export default {
addUserNotice,
getUserNotices,
getNoticeDetails,
generateUserNotice,
removeUserNotice
};
122 changes: 0 additions & 122 deletions src/features/payments/persistence/transactions.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/features/payments/routers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./payment";
import "./wallets";
import "./transactions";
import "./notices";
import "./platform";

export { walletRouter } from "./router";
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import * as O from "fp-ts/lib/Option";
import { pipe } from "fp-ts/lib/function";
import TransactionsDB from "../persistence/transactions";
import NoticesDB from "../persistence/notices";
import { sendFileFromRootPath } from "../../../utils/file";

import { TransactionListWrapResponse } from "../../../../generated/definitions/pagopa/transactions/TransactionListWrapResponse";
import { addTransactionHandler } from "./router";
import { NoticeListWrapResponse } from "../../../../generated/definitions/pagopa/transactions/NoticeListWrapResponse";
import { addNoticesHandler } from "./router";

const CONTINUATION_TOKEN_HEADER = "x-continuation-token";
const DEFAULT_SIZE = 10;

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
)
addNoticesHandler("get", "/paids", (req, res) => {
const size = req.query.size ? Number(req.query.size) : DEFAULT_SIZE;
const offset = isNaN(Number(req.headers[CONTINUATION_TOKEN_HEADER]))
? 0
: Number(req.headers[CONTINUATION_TOKEN_HEADER]);
const response: NoticeListWrapResponse = {
notices: NoticesDB.getUserNotices().slice(offset, offset + size)
};
const continuationToken =
TransactionsDB.getUserTransactions().length > offset + size
NoticesDB.getUserNotices().length > offset + size
? (offset + size).toString()
: undefined;
pipe(
response.transactions,
response.notices,
O.fromNullable,
O.chain(O.fromPredicate(transactions => transactions.length > 0)),
O.fold(
Expand All @@ -46,14 +42,14 @@ addTransactionHandler("get", "/transactions", (req, res) => {
);
});

addTransactionHandler("get", "/transactions/:transactionId", (req, res) => {
addNoticesHandler("get", "/paids/:eventId", (req, res) => {
pipe(
req.params.transactionId,
req.params.eventId,
O.fromNullable,
O.fold(
() => res.sendStatus(400),
transactionId => {
const transaction = TransactionsDB.getTransactionDetails(transactionId);
eventId => {
const transaction = NoticesDB.getNoticeDetails(eventId);
return pipe(
transaction,
O.fold(
Expand All @@ -66,14 +62,14 @@ addTransactionHandler("get", "/transactions/:transactionId", (req, res) => {
);
});

addTransactionHandler("get", "/transactions/:transactionId/pdf", (req, res) => {
addNoticesHandler("get", "/paids/:eventId/pdf", (req, res) => {
pipe(
req.params.transactionId,
req.params.eventId,
O.fromNullable,
O.fold(
() => res.sendStatus(400),
transactionId => {
const transaction = TransactionsDB.getTransactionDetails(transactionId);
eventId => {
const transaction = NoticesDB.getNoticeDetails(eventId);
return pipe(
transaction,
O.fold(
Expand Down
4 changes: 2 additions & 2 deletions src/features/payments/routers/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
getTransactionInfoPayload
} from "../payloads/transactions";
import WalletDB from "../persistence/userWallet";
import TransactionsDB from "../persistence/transactions";
import NoticesDB from "../persistence/notices";
import {
WalletPaymentFailure,
getStatusCodeForWalletFailure
Expand Down Expand Up @@ -210,7 +210,7 @@ addPaymentHandler("post", "/mock-transaction", (req, res) =>
O.fold(
() => res.sendStatus(404),
() => {
TransactionsDB.generateUserTransaction(transactionId, 0);
NoticesDB.generateUserNotice(transactionId, 0);
return res.status(200).json({ status: "ok" });
}
)
Expand Down
10 changes: 4 additions & 6 deletions src/features/payments/routers/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const walletRouter = Router();

export const PAYMENT_WALLET_PREFIX = "/io-payment-wallet/v1";
export const ECOMMERCE_PREFIX = "/ecommerce/io/v2";
export const TRANSACTIONS_PREFIX = "/bizevents/tx-service-jwt/v1";
export const NOTICES_PREFIX = "/bizevents/notices-service-jwt/v1";
export const PLATFORM_PREFIX = "/session-wallet/v1";

export const addPaymentWalletPrefix = (path: string) =>
Expand All @@ -14,8 +14,7 @@ export const addPaymentWalletPrefix = (path: string) =>
export const addECommercePrefix = (path: string) =>
`${ECOMMERCE_PREFIX}${path}`;

export const addTransactionPrefix = (path: string) =>
`${TRANSACTIONS_PREFIX}${path}`;
export const addNoticesPrefix = (path: string) => `${NOTICES_PREFIX}${path}`;

export const addPlatformPrefix = (path: string) => `${PLATFORM_PREFIX}${path}`;

Expand All @@ -32,12 +31,11 @@ export const addPaymentHandler = (
handleRequest: (request: Request, response: Response) => void
) => addHandler(walletRouter, method, addECommercePrefix(path), handleRequest);

export const addTransactionHandler = (
export const addNoticesHandler = (
method: SupportedMethod,
path: string,
handleRequest: (request: Request, response: Response) => void
) =>
addHandler(walletRouter, method, addTransactionPrefix(path), handleRequest);
) => addHandler(walletRouter, method, addNoticesPrefix(path), handleRequest);

export const addPlatformHandler = (
method: SupportedMethod,
Expand Down
Loading
Loading