|
| 1 | +import _ from "lodash"; |
| 2 | +import type { Request, Response } from "express"; |
| 3 | +import uuid from "node-uuid"; |
| 4 | +import HttpStatusCodes from "http-status"; |
| 5 | + |
| 6 | +const nameValidation: RegExp = /^[\p{L}\p{M}\p{N}\p{P}\p{S}\s]+$/u; |
| 7 | + |
| 8 | +const noMatchRegex: RegExp = /no[_ ]match/i; |
| 9 | +const notPossibleRegex: RegExp = /not[_ ]possible/i; |
| 10 | +const closeMatchRegex: RegExp = /close[_ ]match/i; |
| 11 | + |
| 12 | +/** |
| 13 | + * Creates a verification of Payee |
| 14 | + * |
| 15 | + * By default will always return a "MATCH" status. |
| 16 | + * For other statuses use the appropriate regex patterns in the name field. |
| 17 | + * - "NO_MATCH": if name contains "no match" or "no_match" - case insensitive |
| 18 | + * - "VERIFICATION_NOT_POSSIBLE": if name contains "not possible" or "not_possible" - case insensitive |
| 19 | + * - "CLOSE_MATCH": if name contains "close match" or "close_match" - case insensitive |
| 20 | + * |
| 21 | + * @see https://docs.solarisgroup.com/api-reference/digital-banking/sepa-transfers/#tag/Verification-of-Payee/paths/~1v1~1verifications_of_payee/post |
| 22 | + */ |
| 23 | +export const verifyPayee = (req: Request, res: Response) => { |
| 24 | + const data = _.pick(req.body, ["iban", "name"]); |
| 25 | + const { iban, name } = data; |
| 26 | + |
| 27 | + if (!iban) { |
| 28 | + return res.status(HttpStatusCodes.BAD_REQUEST).send({ |
| 29 | + errors: [ |
| 30 | + { |
| 31 | + id: uuid.v4(), |
| 32 | + status: HttpStatusCodes.BAD_REQUEST, |
| 33 | + code: "bad_request", |
| 34 | + title: "Bad Request", |
| 35 | + detail: "IBAN is required.", |
| 36 | + }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + if (!name) { |
| 42 | + return res.status(HttpStatusCodes.BAD_REQUEST).send({ |
| 43 | + errors: [ |
| 44 | + { |
| 45 | + id: uuid.v4(), |
| 46 | + status: HttpStatusCodes.BAD_REQUEST, |
| 47 | + code: "bad_request", |
| 48 | + title: "Bad Request", |
| 49 | + detail: "Name is required.", |
| 50 | + }, |
| 51 | + ], |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + if (!nameValidation.test(name) || name.length > 140) { |
| 56 | + return res.status(HttpStatusCodes.BAD_REQUEST).send({ |
| 57 | + errors: [ |
| 58 | + { |
| 59 | + id: uuid.v4(), |
| 60 | + status: HttpStatusCodes.BAD_REQUEST, |
| 61 | + code: "bad_request", |
| 62 | + title: "Bad Request", |
| 63 | + detail: "Name is not valid.", |
| 64 | + }, |
| 65 | + ], |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + let result: { status: string; suggested_name?: string } = { status: "MATCH" }; |
| 70 | + |
| 71 | + if (noMatchRegex.test(name)) { |
| 72 | + result = { |
| 73 | + status: "NO_MATCH", |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + if (notPossibleRegex.test(name)) { |
| 78 | + result = { |
| 79 | + status: "VERIFICATION_NOT_POSSIBLE", |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + if (closeMatchRegex.test(name)) { |
| 84 | + result = { |
| 85 | + status: "CLOSE_MATCH", |
| 86 | + suggested_name: "Giovanni Kontistini", |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + const dateNow = new Date(); |
| 91 | + const response = { |
| 92 | + id: uuid.v4(), |
| 93 | + payee: { |
| 94 | + iban, |
| 95 | + name, |
| 96 | + }, |
| 97 | + result, |
| 98 | + created_at: dateNow.toISOString(), |
| 99 | + expires_at: new Date(dateNow.getTime() + 5 * 60 * 1000).toISOString(), |
| 100 | + }; |
| 101 | + |
| 102 | + return res.status(HttpStatusCodes.CREATED).send(response); |
| 103 | +}; |
0 commit comments