Skip to content

Commit

Permalink
fix: obizSync route for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Nov 12, 2024
1 parent c588406 commit cb6707c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
3 changes: 1 addition & 2 deletions webapp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
## [0.61.1](https://github.com/SocialGouv/carte-jeune-engage/compare/v0.61.0...v0.61.1) (2024-11-12)


### Bug Fixes

* categories list avatar spacing and placement ([62f80a3](https://github.com/SocialGouv/carte-jeune-engage/commit/62f80a34d4fae11252e5c9419baf82aa3dbaee2c))
- categories list avatar spacing and placement ([62f80a3](https://github.com/SocialGouv/carte-jeune-engage/commit/62f80a34d4fae11252e5c9419baf82aa3dbaee2c))

# [0.61.0](https://github.com/SocialGouv/carte-jeune-engage/compare/v0.60.2...v0.61.0) (2024-11-12)

Expand Down
54 changes: 54 additions & 0 deletions webapp/src/pages/api/obizSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { TRPCError } from "@trpc/server";
import type { NextApiRequest, NextApiResponse } from "next";
import getPayloadClient from "~/payload/payloadClient";
import { appRouter } from "~/server/api/root";
import { createCallerFactory } from "~/server/api/trpc";
import { obiz_soap_client_options } from "~/server/soap-obiz";
var soap = require("soap");

export const obiz_soap_client_url = process.env.OBIZ_SOAP_URL;

const ObizSync = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method !== "POST") {
return res.status(405).send("Invalid request method.");
}

const { numero } = req.query as {
numero: string;
};

try {
const payload = await getPayloadClient({ seed: false });
const createCaller = createCallerFactory(appRouter);

var soapObizClient = await soap.createClientAsync(
obiz_soap_client_url,
obiz_soap_client_options
);

const caller = createCaller({
payload,
session: null,
soapObizClient,
req,
});

const orderId = await caller.order.getIdByNumber({
number: parseInt(numero),
});

const result = await caller.order.synchronizeOrder({
order_id: orderId.data,
});

return res.status(200).json(result);
} catch (error) {
if (error instanceof TRPCError) {
return res.status(400).json({ message: error.message, code: error.code });
} else {
return res.status(500).json({ message: "Internal server error" });
}
}
};

export default ObizSync;
51 changes: 43 additions & 8 deletions webapp/src/server/api/routers/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { TRPCError } from "@trpc/server";
import path from "path";
import { z } from "zod";
import { Media, Offer, Order, Partner, User } from "~/payload/payload-types";
import { createTRPCRouter, userProtectedProcedure } from "~/server/api/trpc";
import {
createTRPCRouter,
publicProcedure,
userProtectedProcedure,
} from "~/server/api/trpc";
import { createOrderPayload, insertItemPayload } from "~/utils/obiz";
import { payloadWhereOfferIsValid } from "~/utils/tools";
import fs from "fs/promises";
Expand Down Expand Up @@ -202,7 +206,7 @@ export const orderRouter = createTRPCRouter({
}
}),

synchronizeOrder: userProtectedProcedure
synchronizeOrder: publicProcedure
.input(
z.object({
order_id: z.number(),
Expand All @@ -217,12 +221,12 @@ export const orderRouter = createTRPCRouter({
depth: 0,
});

if (order.user !== ctx.session.id) {
throw new TRPCError({
code: "FORBIDDEN",
message: `Your are not able to synchronize this order`,
});
}
// if (order.user !== ctx.session.id) {
// throw new TRPCError({
// code: "FORBIDDEN",
// message: `Your are not able to synchronize this order`,
// });
// }

try {
const [resultOrderStatus] =
Expand Down Expand Up @@ -355,6 +359,37 @@ export const orderRouter = createTRPCRouter({
return { data: orders.docs as OrderIncluded[] };
}),

getIdByNumber: publicProcedure
.input(
z.object({
number: z.number(),
})
)
.query(async ({ ctx, input }) => {
const { number } = input;

const orders = await ctx.payload.find({
collection: "orders",
where: {
number: { equals: number },
},
depth: 3,
});

const order = orders.docs[0];

if (!order) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Order not found.",
});
}

return {
data: order.id,
};
}),

getById: userProtectedProcedure
.input(
z.object({
Expand Down

0 comments on commit cb6707c

Please sign in to comment.