Skip to content

Commit

Permalink
feat: init soap obiz client in trpc context and begin order router
Browse files Browse the repository at this point in the history
  • Loading branch information
HoreKk committed Nov 4, 2024
1 parent d51d63d commit abaa53b
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 3 deletions.
2 changes: 2 additions & 0 deletions webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/server/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,6 +32,7 @@ export const appRouter = createTRPCRouter({
searchRequest: searchRequestRouter,
emailAuthToken: emailAuthTokenRouter,
widget: widgetRouter,
order: orderRouter,
});

// export type definition of API
Expand Down
11 changes: 11 additions & 0 deletions webapp/src/server/api/routers/order.ts
Original file line number Diff line number Diff line change
@@ -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" };
}),
});
9 changes: 9 additions & 0 deletions webapp/src/server/api/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,13 +68,19 @@ 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"];

if (!jwtCookie) {
return {
payload,
session: null,
soapObizClient,
req: _opts.req,
};
}
Expand All @@ -82,6 +90,7 @@ export const createTRPCContext = async (_opts: CreateNextContextOptions) => {
return {
payload,
session,
soapObizClient,
req: _opts.req,
};
};
Expand Down
51 changes: 51 additions & 0 deletions webapp/src/server/soap-obiz.ts
Original file line number Diff line number Diff line change
@@ -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(/<s:Envelope[\s\S]*?<\/s:Envelope>/);
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));
},
},
};
Loading

0 comments on commit abaa53b

Please sign in to comment.