Skip to content

Commit

Permalink
fix: solve redirect on iphone and fix offer display when user affected
Browse files Browse the repository at this point in the history
  • Loading branch information
HoreKk committed Mar 13, 2024
1 parent 8364db8 commit cb660a2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 96 deletions.
4 changes: 3 additions & 1 deletion webapp/src/pages/dashboard/offer/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export default function OfferPage() {
} = useDisclosure({
onOpen: () => {
const timeoutId = setTimeout(() => {
window.open(offer?.url as string, "_blank");
setTimeout(() => {
window.open(offer?.url as string, "_blank");
});
onCloseExternalLink();
}, 2000);
setTimeoutIdExternalLink(timeoutId);
Expand Down
197 changes: 102 additions & 95 deletions webapp/src/server/api/routers/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,102 +6,109 @@ import { ZGetListParams } from "~/server/types";
import { payloadWhereOfferIsValid } from "~/utils/tools";

export interface OfferIncluded extends Offer {
partner: Partner & { icon: Media };
category: Category & { icon: Media };
imageOfEligibleStores: Media;
partner: Partner & { icon: Media };
category: Category & { icon: Media };
imageOfEligibleStores: Media;
}

export const offerRouter = createTRPCRouter({
getListOfAvailables: userProtectedProcedure
.input(
ZGetListParams.merge(
z.object({
offerId: z.number().optional(),
categoryId: z.number().optional(),
isCurrentUser: z.boolean().optional(),
})
)
)
.query(async ({ ctx, input }) => {
const { perPage, page, sort, categoryId, offerId, isCurrentUser } = input;

let where = {
...payloadWhereOfferIsValid(),
} as Where;

if (categoryId) {
where.category = {
equals: categoryId,
};
}

if (offerId) {
where.id = {
equals: offerId,
};
}

const offers = await ctx.payload.find({
collection: "offers",
limit: perPage,
page: page,
where: where as Where,
sort,
});

const myUnusedCoupons = await ctx.payload.find({
collection: "coupons",
depth: 0,
limit: 1000,
where: {
used: { equals: false },
user: { equals: ctx.session.id }
},
});

const couponCountOfOffersPromises = offers.docs.map((offer) => ctx.payload.find({
collection: "coupons",
limit: 1,
where: {
offer: {
equals: offer.id,
},
used: { equals: false },
user: { exists: false }
},
}));

const couponCountOfOffers = await Promise.all(couponCountOfOffersPromises)

const offersFiltered = offers.docs.filter((offer, index) => {
const myUnusedOfferCoupons = myUnusedCoupons.docs.find((coupon) => coupon.offer === offer.id)

if (!isCurrentUser && (offer.kind === "voucher_pass" || offer.kind === "code_space"))
return true;
else if (isCurrentUser)
return !!myUnusedOfferCoupons


const coupons = couponCountOfOffers[index]
return !!coupons && !!coupons.docs.length;
});

return {
data: offersFiltered as OfferIncluded[],
metadata: { page, count: offers.docs.length },
};
}),

getById: userProtectedProcedure
.input(z.object({ id: z.number() }))
.query(async ({ ctx, input }) => {
const { id } = input;

const offer = await ctx.payload.findByID({
collection: "offers",
id,
});

return { data: offer as OfferIncluded };
}),
getListOfAvailables: userProtectedProcedure
.input(
ZGetListParams.merge(
z.object({
offerId: z.number().optional(),
categoryId: z.number().optional(),
isCurrentUser: z.boolean().optional(),
})
)
)
.query(async ({ ctx, input }) => {
const { perPage, page, sort, categoryId, offerId, isCurrentUser } = input;

let where = {
...payloadWhereOfferIsValid(),
} as Where;

if (categoryId) {
where.category = {
equals: categoryId,
};
}

if (offerId) {
where.id = {
equals: offerId,
};
}

const offers = await ctx.payload.find({
collection: "offers",
limit: perPage,
page: page,
where: where as Where,
sort,
});

const myUnusedCoupons = await ctx.payload.find({
collection: "coupons",
depth: 0,
limit: 1000,
where: {
used: { equals: false },
user: { equals: ctx.session.id },
},
});

const couponCountOfOffersPromises = offers.docs.map((offer) =>
ctx.payload.find({
collection: "coupons",
limit: 1,
where: {
offer: {
equals: offer.id,
},
used: { equals: false },
user: { exists: false },
},
})
);

const couponCountOfOffers = await Promise.all(
couponCountOfOffersPromises
);

const offersFiltered = offers.docs.filter((offer, index) => {
const myUnusedOfferCoupons = myUnusedCoupons.docs.find(
(coupon) => coupon.offer === offer.id
);

if (
!isCurrentUser &&
(offer.kind === "voucher_pass" || offer.kind === "code_space")
)
return true;
else if (myUnusedOfferCoupons) return true;

const coupons = couponCountOfOffers[index];
return !!coupons && !!coupons.docs.length;
});

return {
data: offersFiltered as OfferIncluded[],
metadata: { page, count: offers.docs.length },
};
}),

getById: userProtectedProcedure
.input(z.object({ id: z.number() }))
.query(async ({ ctx, input }) => {
const { id } = input;

const offer = await ctx.payload.findByID({
collection: "offers",
id,
});

return { data: offer as OfferIncluded };
}),
});

0 comments on commit cb660a2

Please sign in to comment.