diff --git a/webapp/CHANGELOG.md b/webapp/CHANGELOG.md index 6659046e..29ed3ad8 100644 --- a/webapp/CHANGELOG.md +++ b/webapp/CHANGELOG.md @@ -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) diff --git a/webapp/src/pages/api/obizSync.ts b/webapp/src/pages/api/obizSync.ts new file mode 100644 index 00000000..521d3f96 --- /dev/null +++ b/webapp/src/pages/api/obizSync.ts @@ -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; diff --git a/webapp/src/server/api/routers/order.ts b/webapp/src/server/api/routers/order.ts index feada54a..39d6d362 100644 --- a/webapp/src/server/api/routers/order.ts +++ b/webapp/src/server/api/routers/order.ts @@ -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"; @@ -202,7 +206,7 @@ export const orderRouter = createTRPCRouter({ } }), - synchronizeOrder: userProtectedProcedure + synchronizeOrder: publicProcedure .input( z.object({ order_id: z.number(), @@ -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] = @@ -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({