-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add page for widget redirect on offer to add source to redirecti…
…on url
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |