Skip to content

Commit

Permalink
feat: add new collection coupon, add url to partner and coupon router
Browse files Browse the repository at this point in the history
  • Loading branch information
HoreKk committed Jan 24, 2024
1 parent d51c6c3 commit 577b02f
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 8 deletions.
8 changes: 1 addition & 7 deletions webapp/src/pages/dashboard/offer/online/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
Flex,
Heading,
Icon,
IconButton,
List,
ListIcon,
ListItem,
Expand All @@ -22,12 +21,7 @@ import {
} from "@chakra-ui/react";
import Image from "next/image";
import { api } from "~/utils/api";
import {
ArrowForwardIcon,
CheckIcon,
ChevronLeftIcon,
CloseIcon,
} from "@chakra-ui/icons";
import { ArrowForwardIcon, CheckIcon, ChevronLeftIcon } from "@chakra-ui/icons";
import { useRouter } from "next/router";
import { dottedPattern } from "~/utils/chakra-theme";
import { CouponIcon } from "~/components/icons/coupon";
Expand Down
50 changes: 50 additions & 0 deletions webapp/src/payload/collections/Coupon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { type CollectionConfig } from "payload/types";

export const Coupons: CollectionConfig = {
slug: "coupons",
labels: {
singular: "Bon de réduction",
plural: "Bons de réduction",
},
fields: [
{
name: "code",
type: "text",
label: "Code",
required: true,
unique: true,
},
{
name: "status",
type: "select",
label: "Statut",
options: [
{ label: "Disponible", value: "available" },
{ label: "Archivé", value: "archived" },
],
defaultValue: "available",
required: true,
},
{
name: "validityTo",
type: "date",
label: "Validité jusqu'au",
required: true,
},
{
name: "user",
type: "relationship",
label: "Utilisateur",
relationTo: "users",
hasMany: false,
},
{
name: "offer",
type: "relationship",
label: "Offre",
relationTo: "offers",
hasMany: false,
required: true,
},
],
};
6 changes: 6 additions & 0 deletions webapp/src/payload/collections/Partner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export const Partners: CollectionConfig = {
label: "Description",
required: true,
},
{
name: "url",
type: "text",
label: "URL",
required: true,
},
{
name: "color",
type: "text",
Expand Down
12 changes: 12 additions & 0 deletions webapp/src/payload/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Config {
categories: Category;
partners: Partner;
offers: Offer;
coupons: Coupon;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
Expand Down Expand Up @@ -73,6 +74,7 @@ export interface Partner {
id: number;
name: string;
description: string;
url: string;
color: string;
icon: number | Media;
updatedAt: string;
Expand All @@ -87,6 +89,16 @@ export interface Offer {
updatedAt: string;
createdAt: string;
}
export interface Coupon {
id: number;
code: string;
status: 'available' | 'archived';
validityTo: string;
user?: (number | null) | User;
offer: number | Offer;
updatedAt: string;
createdAt: string;
}
export interface PayloadPreference {
id: number;
user:
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/payload/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Media } from "./collections/Media";
import { Categories } from "./collections/Categorie";
import { Partners } from "./collections/Partner";
import { Offers } from "./collections/Offer";
import { Coupons } from "./collections/Coupon";

const adapter = s3Adapter({
config: {
Expand Down Expand Up @@ -51,7 +52,7 @@ export default buildConfig({
bundler: webpackBundler(),
user: "admins",
},
collections: [Admins, Users, Media, Categories, Partners, Offers],
collections: [Admins, Users, Media, Categories, Partners, Offers, Coupons],
localization: {
locales: ["fr"],
defaultLocale: "fr",
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/payload/seed/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ export const partners = [
name: "Cora",
description: "Description de Cora",
color: "#283F93",
url: "https://www.cora.fr/",
},
{
name: "Lidl",
description: "Description de Lidl",
color: "#FFF000",
url: "https://www.lidl.fr/",
},
{
name: "Auchan",
description: "Description de Auchan",
color: "#E0001A",
url: "https://www.auchan.fr/",
},
{
name: "Flixbus",
description: "Description de Flixbus",
color: "#73D700",
url: "https://www.flixbus.fr/",
},
] as const;

Expand Down
2 changes: 2 additions & 0 deletions webapp/src/server/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createTRPCRouter } from "~/server/api/trpc";
import { userRouter } from "./routers/user";
import { categoryRouter } from "./routers/category";
import { offerRouter } from "./routers/offer";
import { couponRouter } from "./routers/coupon";

/**
* This is the primary router for your server.
Expand All @@ -13,6 +14,7 @@ export const appRouter = createTRPCRouter({
user: userRouter,
category: categoryRouter,
offer: offerRouter,
coupon: couponRouter,
});

// export type definition of API
Expand Down
29 changes: 29 additions & 0 deletions webapp/src/server/api/routers/coupon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { z } from "zod";
import { Coupon, Media, Offer, User } from "~/payload/payload-types";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";

interface CouponIncluded extends Coupon {
offer: Offer & { icon: Media };
userRouter: User;
}

export const couponRouter = createTRPCRouter({
getOneAvailable: protectedProcedure
.input(z.object({ offer_id: z.number() }))
.query(async ({ ctx, input }) => {
const { offer_id } = input;

const coupons = await ctx.payload.find({
collection: "coupons",
where: {
and: [
{ offer: { equals: offer_id } },
{ status: { equals: "available" } },
{ validityTo: { greater_than_equal: new Date() } },
],
},
});

return { data: coupons.docs[0] as CouponIncluded };
}),
});

0 comments on commit 577b02f

Please sign in to comment.