-
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.
feat: attach coupon on activation, add interactivity with current cou…
…pon for partner url, copy of coupon code and validity from offer
- Loading branch information
Showing
7 changed files
with
106 additions
and
36 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
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
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
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
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
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 |
---|---|---|
@@ -1,29 +1,64 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import { z } from "zod"; | ||
import { Coupon, Media, Offer, User } from "~/payload/payload-types"; | ||
import { Coupon, Media, Offer, Partner, User } from "~/payload/payload-types"; | ||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc"; | ||
|
||
interface CouponIncluded extends Coupon { | ||
offer: Offer & { icon: Media }; | ||
offer: Offer & { icon: Media; partner: Partner }; | ||
userRouter: User; | ||
} | ||
|
||
export const couponRouter = createTRPCRouter({ | ||
getOneAvailable: protectedProcedure | ||
getOne: protectedProcedure | ||
.input(z.object({ offer_id: z.number() })) | ||
.query(async ({ ctx, input }) => { | ||
const { offer_id } = input; | ||
|
||
const coupons = await ctx.payload.find({ | ||
collection: "coupons", | ||
depth: 2, | ||
where: { | ||
and: [ | ||
{ offer: { equals: offer_id } }, | ||
{ status: { equals: "available" } }, | ||
{ validityTo: { greater_than_equal: new Date() } }, | ||
{ user: { equals: ctx.session.id } }, | ||
], | ||
}, | ||
}); | ||
|
||
return { data: coupons.docs[0] as CouponIncluded }; | ||
}), | ||
|
||
assignToUser: protectedProcedure | ||
.input(z.object({ offer_id: z.number() })) | ||
.mutation(async ({ ctx, input }) => { | ||
const { offer_id } = input; | ||
|
||
const coupon = await ctx.payload.find({ | ||
collection: "coupons", | ||
where: { | ||
and: [ | ||
{ offer: { equals: offer_id } }, | ||
{ status: { equals: "available" } }, | ||
], | ||
}, | ||
}); | ||
|
||
if (coupon.docs.length === 0) { | ||
throw new TRPCError({ | ||
code: "NOT_FOUND", | ||
message: "Coupon not found", | ||
}); | ||
} | ||
|
||
const couponData = coupon.docs[0] as CouponIncluded; | ||
|
||
const updatedCoupon = await ctx.payload.update({ | ||
collection: "coupons", | ||
id: couponData.id, | ||
data: { user: ctx.session.id }, | ||
}); | ||
|
||
return { data: updatedCoupon }; | ||
}), | ||
}); |
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