From abaa53b6045d9d2fc8f0354db858d3031bf31e8c Mon Sep 17 00:00:00 2001 From: Antoine Lelong Date: Mon, 4 Nov 2024 13:52:47 +0100 Subject: [PATCH] feat: init soap obiz client in trpc context and begin order router --- webapp/package.json | 2 + webapp/src/server/api/root.ts | 2 + webapp/src/server/api/routers/order.ts | 11 ++ webapp/src/server/api/trpc.ts | 9 ++ webapp/src/server/soap-obiz.ts | 51 ++++++++ webapp/yarn.lock | 162 ++++++++++++++++++++++++- 6 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 webapp/src/server/api/routers/order.ts create mode 100644 webapp/src/server/soap-obiz.ts diff --git a/webapp/package.json b/webapp/package.json index 81d8128f..ff902ff1 100644 --- a/webapp/package.json +++ b/webapp/package.json @@ -39,6 +39,7 @@ "@trpc/react-query": "^10.44.1", "@trpc/server": "^10.44.1", "aws-crt": "^1.20.1", + "axios": "^1.7.7", "bowser": "^2.11.0", "cookies-next": "^4.1.0", "crisp-sdk-web": "^1.0.21", @@ -64,6 +65,7 @@ "react-qrcode-logo": "^2.9.0", "react-toastify": "^10.0.5", "sharp": "^0.33.2", + "soap": "^1.1.6", "superjson": "^1.13.3", "tsx": "^4.7.0", "usehooks-ts": "^2.14.0", diff --git a/webapp/src/server/api/root.ts b/webapp/src/server/api/root.ts index 17f04302..c5802ffb 100644 --- a/webapp/src/server/api/root.ts +++ b/webapp/src/server/api/root.ts @@ -12,6 +12,7 @@ import { tagRouter } from "./routers/tag"; import { searchRequestRouter } from "./routers/searchRequest"; import { emailAuthTokenRouter } from "./routers/emailAuthToken"; import { widgetRouter } from "./routers/widget"; +import { orderRouter } from "./routers/order"; /** * This is the primary router for your server. @@ -31,6 +32,7 @@ export const appRouter = createTRPCRouter({ searchRequest: searchRequestRouter, emailAuthToken: emailAuthTokenRouter, widget: widgetRouter, + order: orderRouter, }); // export type definition of API diff --git a/webapp/src/server/api/routers/order.ts b/webapp/src/server/api/routers/order.ts new file mode 100644 index 00000000..0f6ebe3b --- /dev/null +++ b/webapp/src/server/api/routers/order.ts @@ -0,0 +1,11 @@ +import { createTRPCRouter, userProtectedProcedure } from "~/server/api/trpc"; + +export const orderRouter = createTRPCRouter({ + createOrder: userProtectedProcedure.query(async ({ ctx }) => { + // const [test] = await ctx.soapObizClient.ETAT_SITEAsync(); + + // console.log(JSON.stringify(test, null, 2)); + + return { data: "Hello" }; + }), +}); diff --git a/webapp/src/server/api/trpc.ts b/webapp/src/server/api/trpc.ts index e7b014ce..8ec66df4 100644 --- a/webapp/src/server/api/trpc.ts +++ b/webapp/src/server/api/trpc.ts @@ -17,6 +17,8 @@ import jwt from "jsonwebtoken"; import { Payload } from "payload"; import { NextApiRequest } from "next"; import { ZWidgetToken } from "../types"; +var soap = require("soap"); +import { obiz_soap_client_options, obiz_soap_client_url } from "../soap-obiz"; export type PayloadJwtSession = { id: number; @@ -66,6 +68,11 @@ export const createTRPCContext = async (_opts: CreateNextContextOptions) => { seed: false, }); + var soapObizClient = await soap.createClientAsync( + obiz_soap_client_url, + obiz_soap_client_options + ); + const jwtCookie = _opts.req.cookies[process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"]; @@ -73,6 +80,7 @@ export const createTRPCContext = async (_opts: CreateNextContextOptions) => { return { payload, session: null, + soapObizClient, req: _opts.req, }; } @@ -82,6 +90,7 @@ export const createTRPCContext = async (_opts: CreateNextContextOptions) => { return { payload, session, + soapObizClient, req: _opts.req, }; }; diff --git a/webapp/src/server/soap-obiz.ts b/webapp/src/server/soap-obiz.ts new file mode 100644 index 00000000..d49728e0 --- /dev/null +++ b/webapp/src/server/soap-obiz.ts @@ -0,0 +1,51 @@ +import { IOptions } from "soap"; + +export const obiz_soap_client_url = + "https://webservices-test.reducce.fr/Partenaire.svc?wsdl"; + +function parseMultipartResponse(data: string) { + // Find the XML content between the MIME boundaries + const xmlMatch = data.match(//); + if (xmlMatch) { + return xmlMatch[0]; + } + throw new Error("No SOAP envelope found in response"); +} + +export const obiz_soap_client_options: IOptions = { + forceSoap12Headers: false, + endpoint: "https://webservices-test.reducce.fr/Partenaire.svc/Partenaire.svc", + httpClient: { + request: function ( + url: string, + data: string, + callback: any, + exheaders: any + ): any { + const headers = { + "Content-Type": "text/xml;charset=utf-8", + Accept: "multipart/related,text/xml", + ...exheaders, + }; + + const options = { + url: url, + method: data ? "POST" : "GET", + headers: headers, + data: data, + responseType: "text", + }; + + require("axios")(options) + .then((response: any) => { + if (response.headers["content-type"]?.includes("multipart/related")) { + // Parse multipart response to get just the SOAP envelope + const xmlContent = parseMultipartResponse(response.data); + response.data = xmlContent; + } + callback(null, response, response.data); + }) + .catch((error: any) => callback(error)); + }, + }, +}; diff --git a/webapp/yarn.lock b/webapp/yarn.lock index 6db57a2e..85e004f9 100644 --- a/webapp/yarn.lock +++ b/webapp/yarn.lock @@ -5765,6 +5765,20 @@ __metadata: languageName: node linkType: hard +"@xmldom/is-dom-node@npm:^1.0.1": + version: 1.0.1 + resolution: "@xmldom/is-dom-node@npm:1.0.1" + checksum: 10c0/138d5e74441b16f065ce360d81737673986d93f14d5bb09b1e3bcfc2b18fae70b86beb9b7bfbffe916dd36b3bdab012acaa81cc0b49450acadfd66978b62827f + languageName: node + linkType: hard + +"@xmldom/xmldom@npm:^0.8.10": + version: 0.8.10 + resolution: "@xmldom/xmldom@npm:0.8.10" + checksum: 10c0/c7647c442502720182b0d65b17d45d2d95317c1c8c497626fe524bda79b4fb768a9aa4fae2da919f308e7abcff7d67c058b102a9d641097e9a57f0b80187851f + languageName: node + linkType: hard + "@xtuc/ieee754@npm:^1.2.0": version: 1.2.0 resolution: "@xtuc/ieee754@npm:1.2.0" @@ -6051,6 +6065,13 @@ __metadata: languageName: node linkType: hard +"asap@npm:^2.0.0": + version: 2.0.6 + resolution: "asap@npm:2.0.6" + checksum: 10c0/c6d5e39fe1f15e4b87677460bd66b66050cd14c772269cee6688824c1410a08ab20254bb6784f9afb75af9144a9f9a7692d49547f4d19d715aeb7c0318f3136d + languageName: node + linkType: hard + "asn1.js@npm:^5.3.0": version: 5.4.1 resolution: "asn1.js@npm:5.4.1" @@ -6126,7 +6147,19 @@ __metadata: languageName: node linkType: hard -"axios@npm:^1.7.4": +"axios-ntlm@npm:^1.4.2": + version: 1.4.2 + resolution: "axios-ntlm@npm:1.4.2" + dependencies: + axios: "npm:^1.6.1" + des.js: "npm:^1.1.0" + dev-null: "npm:^0.1.1" + js-md4: "npm:^0.3.2" + checksum: 10c0/21b1a5ce488937c0e90129840655833ed721e8e20e00d72ba72ff9c2ce2455343f37a95b5475ec46455e183260f658d2e72df705c23f9629655d6de742da9221 + languageName: node + linkType: hard + +"axios@npm:^1.6.1, axios@npm:^1.7.4, axios@npm:^1.7.7": version: 1.7.7 resolution: "axios@npm:1.7.7" dependencies: @@ -7204,6 +7237,18 @@ __metadata: languageName: node linkType: hard +"debug@npm:^4.3.6": + version: 4.3.7 + resolution: "debug@npm:4.3.7" + dependencies: + ms: "npm:^2.1.3" + peerDependenciesMeta: + supports-color: + optional: true + checksum: 10c0/1471db19c3b06d485a622d62f65947a19a23fbd0dd73f7fd3eafb697eec5360cde447fb075919987899b1a2096e85d35d4eb5a4de09a57600ac9cf7e6c8e768b + languageName: node + linkType: hard + "decompress-response@npm:^6.0.0": version: 6.0.0 resolution: "decompress-response@npm:6.0.0" @@ -7320,6 +7365,16 @@ __metadata: languageName: node linkType: hard +"des.js@npm:^1.1.0": + version: 1.1.0 + resolution: "des.js@npm:1.1.0" + dependencies: + inherits: "npm:^2.0.1" + minimalistic-assert: "npm:^1.0.0" + checksum: 10c0/671354943ad67493e49eb4c555480ab153edd7cee3a51c658082fcde539d2690ed2a4a0b5d1f401f9cde822edf3939a6afb2585f32c091f2d3a1b1665cd45236 + languageName: node + linkType: hard + "destroy@npm:1.2.0": version: 1.2.0 resolution: "destroy@npm:1.2.0" @@ -7355,6 +7410,23 @@ __metadata: languageName: node linkType: hard +"dev-null@npm:^0.1.1": + version: 0.1.1 + resolution: "dev-null@npm:0.1.1" + checksum: 10c0/189223075b837c36adb05663ff16991a206cd85aaf3fb44e34be91c0858787c583b18683b581446dc40101989d9aaaf5a49c02bc2200c0955b07d833252ef86e + languageName: node + linkType: hard + +"dezalgo@npm:^1.0.4": + version: 1.0.4 + resolution: "dezalgo@npm:1.0.4" + dependencies: + asap: "npm:^2.0.0" + wrappy: "npm:1" + checksum: 10c0/8a870ed42eade9a397e6141fe5c025148a59ed52f1f28b1db5de216b4d57f0af7a257070c3af7ce3d5508c1ce9dd5009028a76f4b2cc9370dc56551d2355fad8 + languageName: node + linkType: hard + "diff@npm:^5.1.0": version: 5.2.0 resolution: "diff@npm:5.2.0" @@ -8553,6 +8625,17 @@ __metadata: languageName: node linkType: hard +"formidable@npm:^3.5.1": + version: 3.5.2 + resolution: "formidable@npm:3.5.2" + dependencies: + dezalgo: "npm:^1.0.4" + hexoid: "npm:^2.0.0" + once: "npm:^1.4.0" + checksum: 10c0/c26d89ba84d392f0e68ba1aca9f779e0f2e94db053d95df562c730782956f302e3f069c07ab96f991415af915ac7b8771f4c813d298df43577fdf439e1e8741e + languageName: node + linkType: hard + "forwarded@npm:0.2.0": version: 0.2.0 resolution: "forwarded@npm:0.2.0" @@ -8713,6 +8796,13 @@ __metadata: languageName: node linkType: hard +"get-stream@npm:^6.0.1": + version: 6.0.1 + resolution: "get-stream@npm:6.0.1" + checksum: 10c0/49825d57d3fd6964228e6200a58169464b8e8970489b3acdc24906c782fb7f01f9f56f8e6653c4a50713771d6658f7cfe051e5eb8c12e334138c9c918b296341 + languageName: node + linkType: hard + "get-tsconfig@npm:4.6.2": version: 4.6.2 resolution: "get-tsconfig@npm:4.6.2" @@ -9008,6 +9098,13 @@ __metadata: languageName: node linkType: hard +"hexoid@npm:^2.0.0": + version: 2.0.0 + resolution: "hexoid@npm:2.0.0" + checksum: 10c0/a9d5e6f4adeaefcb4a53803dd48bf0a242d92e8ec699555aea616c4bf8f91788f03093595085976f63d6830815dd080c063503540fabc7e854ebfb11161687c6 + languageName: node + linkType: hard + "history@npm:^4.9.0": version: 4.10.1 resolution: "history@npm:4.10.1" @@ -9847,6 +9944,13 @@ __metadata: languageName: node linkType: hard +"js-md4@npm:^0.3.2": + version: 0.3.2 + resolution: "js-md4@npm:0.3.2" + checksum: 10c0/8313e00c45f710a53bdadc199c095b48ebaf54ea7b8cdb67a3f1863c270a5e9d0f89f204436b73866002af8c7ac4cacc872fdf271fc70e26614e424c7685b577 + languageName: node + linkType: hard + "js-sdsl@npm:4.3.0": version: 4.3.0 resolution: "js-sdsl@npm:4.3.0" @@ -10761,7 +10865,7 @@ __metadata: languageName: node linkType: hard -"ms@npm:2.1.3, ms@npm:^2.1.1": +"ms@npm:2.1.3, ms@npm:^2.1.1, ms@npm:^2.1.3": version: 2.1.3 resolution: "ms@npm:2.1.3" checksum: 10c0/d924b57e7312b3b63ad21fc5b3dc0af5e78d61a1fc7cfb5457edaf26326bf62be5307cc87ffb6862ef1c2b33b0233cdb5d4f01c4c958cc0d660948b65a287a48 @@ -13318,7 +13422,7 @@ __metadata: languageName: node linkType: hard -"sax@npm:^1.2.4": +"sax@npm:^1.2.4, sax@npm:^1.4.1": version: 1.4.1 resolution: "sax@npm:1.4.1" checksum: 10c0/6bf86318a254c5d898ede6bd3ded15daf68ae08a5495a2739564eb265cd13bcc64a07ab466fb204f67ce472bb534eb8612dac587435515169593f4fffa11de7c @@ -13747,6 +13851,24 @@ __metadata: languageName: node linkType: hard +"soap@npm:^1.1.6": + version: 1.1.6 + resolution: "soap@npm:1.1.6" + dependencies: + axios: "npm:^1.7.7" + axios-ntlm: "npm:^1.4.2" + debug: "npm:^4.3.6" + formidable: "npm:^3.5.1" + get-stream: "npm:^6.0.1" + lodash: "npm:^4.17.21" + sax: "npm:^1.4.1" + strip-bom: "npm:^3.0.0" + whatwg-mimetype: "npm:4.0.0" + xml-crypto: "npm:^6.0.0" + checksum: 10c0/8cd41718c76b0c09cc65728466a34051ec96077e350da9a736430899b1401ff98c867659953ca5b954ca8c68ca52a2577f9c49b35e88f99f55598b6923e91929 + languageName: node + linkType: hard + "socks-proxy-agent@npm:^8.0.3": version: 8.0.4 resolution: "socks-proxy-agent@npm:8.0.4" @@ -13985,6 +14107,13 @@ __metadata: languageName: node linkType: hard +"strip-bom@npm:^3.0.0": + version: 3.0.0 + resolution: "strip-bom@npm:3.0.0" + checksum: 10c0/51201f50e021ef16672593d7434ca239441b7b760e905d9f33df6e4f3954ff54ec0e0a06f100d028af0982d6f25c35cd5cda2ce34eaebccd0250b8befb90d8f1 + languageName: node + linkType: hard + "strip-json-comments@npm:^3.1.1": version: 3.1.1 resolution: "strip-json-comments@npm:3.1.1" @@ -14861,6 +14990,7 @@ __metadata: "@types/react-dom": "npm:^18" "@types/web-push": "npm:^3" aws-crt: "npm:^1.20.1" + axios: "npm:^1.7.7" bowser: "npm:^2.11.0" cookies-next: "npm:^4.1.0" crisp-sdk-web: "npm:^1.0.21" @@ -14888,6 +15018,7 @@ __metadata: react-toastify: "npm:^10.0.5" serwist: "npm:^9.0.2" sharp: "npm:^0.33.2" + soap: "npm:^1.1.6" superjson: "npm:^1.13.3" tsx: "npm:^4.7.0" typescript: "npm:5.4.5" @@ -15076,6 +15207,13 @@ __metadata: languageName: node linkType: hard +"whatwg-mimetype@npm:4.0.0": + version: 4.0.0 + resolution: "whatwg-mimetype@npm:4.0.0" + checksum: 10c0/a773cdc8126b514d790bdae7052e8bf242970cebd84af62fb2f35a33411e78e981f6c0ab9ed1fe6ec5071b09d5340ac9178e05b52d35a9c4bcf558ba1b1551df + languageName: node + linkType: hard + "whatwg-url@npm:^5.0.0": version: 5.0.0 resolution: "whatwg-url@npm:5.0.0" @@ -15275,6 +15413,24 @@ __metadata: languageName: node linkType: hard +"xml-crypto@npm:^6.0.0": + version: 6.0.0 + resolution: "xml-crypto@npm:6.0.0" + dependencies: + "@xmldom/is-dom-node": "npm:^1.0.1" + "@xmldom/xmldom": "npm:^0.8.10" + xpath: "npm:^0.0.33" + checksum: 10c0/1a9d8be4cc7a4c618fa413b8ef30f11cda9ae81f20bc03e84c51f6c61383168a9915f8c3a26061e2053e58807b76d3a13726338f7bc0d8c45285fbb1da296293 + languageName: node + linkType: hard + +"xpath@npm:^0.0.33": + version: 0.0.33 + resolution: "xpath@npm:0.0.33" + checksum: 10c0/ac2c04142c0f38e75f0d899b6818b08a0e8163aab5d6fd8a292f31a6e925ab08ee48feb1f447049c5bbcb8926b7241c79d1d4a51386e6f6f2d76ac5784917b9d + languageName: node + linkType: hard + "xss@npm:^1.0.6": version: 1.0.15 resolution: "xss@npm:1.0.15"