Skip to content

Commit

Permalink
fix: add page for widget redirect on offer to add source to redirecti…
Browse files Browse the repository at this point in the history
…on url
  • Loading branch information
HoreKk committed Nov 20, 2024
1 parent 54148f8 commit 4213d23
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions webapp/src/pages/dashboard/offer/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { jwtDecode } from "jwt-decode";
import { GetServerSideProps, NextApiRequest } from "next";
import getPayloadClient from "~/payload/payloadClient";
import { appRouter } from "~/server/api/root";
import { createCallerFactory, PayloadJwtSession } from "~/server/api/trpc";

export const getServerSideProps: GetServerSideProps = async ({
query,
req,
}) => {
const order_id = query.id;

const jwtCookie = req.cookies[process.env.NEXT_PUBLIC_JWT_NAME ?? "cje-jwt"];

if (!order_id || typeof order_id !== "string" || !jwtCookie) {
return {
redirect: {
destination: "/dashboard",
permanent: false,
},
};
}

const payload = await getPayloadClient({ seed: false });

const session = jwtDecode<PayloadJwtSession>(jwtCookie);

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

try {
const { data: offers } = await caller.offer.getListOfAvailables({
page: 1,
perPage: 1,
offerIds: [parseInt(order_id)],
});

if (offers.length !== 1) {
return {
redirect: {
destination: "/dashboard",
permanent: false,
},
};
}

const currentOffer = offers[0];

return {
redirect: {
destination: `/dashboard/offer/${currentOffer.source}/${order_id}`,
permanent: false,
},
};
} catch (error) {
console.log("Error in offer redirect :", error);
return {
redirect: {
destination: "/dashboard",
permanent: false,
},
};
}
};

export const OfferRedirect = () => {
return null;
};

export default OfferRedirect;

0 comments on commit 4213d23

Please sign in to comment.