Skip to content

Commit

Permalink
chore: [IOBP-927] Adapt new openAPI biz-events (#423)
Browse files Browse the repository at this point in the history
* chore: Adapt new openAPI biz-events

* chore: Changed route

* chore: edit baseurl

* chore: edit api path from transactions to paids

* chore: Replaced `transaction` occurrences with `notice`

* chore: Applied GH suggestions from @LeleDallas

* fix: test ci

* chore: Remove eslinter comment adding spread operator modifier

* chore: Removed eslint comment with the GH suggestion code
  • Loading branch information
Hantex9 authored Oct 17, 2024
1 parent 584a724 commit b5de21d
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 164 deletions.
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
119 changes: 119 additions & 0 deletions src/features/payments/persistence/notices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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()
})
);
const updatedNotice = {
...randomNotice,
isCart: cartList.length > 1,
amount: cartList
.reduce((acc, item) => acc + Number(item.amount), 0)
.toString()
};
addUserNotice(updatedNotice);

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

const generateUserNoticeData = () => {
for (const i of Array(
ioDevServerConfig.features.payments.numberOfTransactions
).keys()) {
generateUserNotice(faker.datatype.uuid(), i);
}
};

// 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

0 comments on commit b5de21d

Please sign in to comment.