|
| 1 | +import { type MatchContext } from "@deco/deco/blocks"; |
| 2 | +import { parseAuthCookie } from "../utils/vtexId.ts"; |
| 3 | + |
| 4 | +/** @title Anonymous without cart */ |
| 5 | +interface AnonymousWithoutCart { |
| 6 | + /** |
| 7 | + * @hide true |
| 8 | + */ |
| 9 | + segment: "anonymous-without-cart"; |
| 10 | +} |
| 11 | + |
| 12 | +/** @title Anonymous with cart */ |
| 13 | +interface AnonymousWithCart { |
| 14 | + /** |
| 15 | + * @hide true |
| 16 | + */ |
| 17 | + segment: "anonymous-with-cart"; |
| 18 | +} |
| 19 | + |
| 20 | +/** @title Logged in */ |
| 21 | +interface LoggedIn { |
| 22 | + /** |
| 23 | + * @hide true |
| 24 | + */ |
| 25 | + segment: "logged-in"; |
| 26 | +} |
| 27 | + |
| 28 | +/** @title Logged in without orders */ |
| 29 | +interface LoggedInWithoutOrders { |
| 30 | + /** |
| 31 | + * @hide true |
| 32 | + */ |
| 33 | + segment: "logged-in-without-orders"; |
| 34 | +} |
| 35 | + |
| 36 | +/** @title Logged in with orders */ |
| 37 | +interface LoggedInWithOrders { |
| 38 | + /** |
| 39 | + * @hide true |
| 40 | + */ |
| 41 | + segment: "logged-in-with-orders"; |
| 42 | +} |
| 43 | + |
| 44 | +/** @title Logged in with recent orders */ |
| 45 | +interface LoggedInWithRecentOrders { |
| 46 | + /** |
| 47 | + * @hide true |
| 48 | + */ |
| 49 | + segment: "logged-in-with-recent-orders"; |
| 50 | + /** |
| 51 | + * @title Months |
| 52 | + * @description Number of months to consider an order as "recent" |
| 53 | + * @default 3 |
| 54 | + */ |
| 55 | + months?: number; |
| 56 | +} |
| 57 | + |
| 58 | +export type Props = |
| 59 | + | AnonymousWithoutCart |
| 60 | + | AnonymousWithCart |
| 61 | + | LoggedIn |
| 62 | + | LoggedInWithoutOrders |
| 63 | + | LoggedInWithOrders |
| 64 | + | LoggedInWithRecentOrders; |
| 65 | + |
| 66 | +/** |
| 67 | + * @title User Segment |
| 68 | + * @description Segment users by authentication status, cart state, and order history |
| 69 | + * @icon user-check |
| 70 | + */ |
| 71 | +const MatchUserSegment = async ( |
| 72 | + props: Props, |
| 73 | + { request, invoke }: MatchContext, |
| 74 | +): Promise<boolean> => { |
| 75 | + const { segment } = props; |
| 76 | + // deno-lint-ignore no-explicit-any |
| 77 | + const vtex = (invoke as any).vtex; |
| 78 | + |
| 79 | + const payload = parseAuthCookie(request.headers); |
| 80 | + const isLoggedIn = Boolean(payload?.sub && payload?.userId); |
| 81 | + |
| 82 | + try { |
| 83 | + if (segment === "anonymous-without-cart") { |
| 84 | + if (isLoggedIn) return false; |
| 85 | + const cart = await vtex.loaders.cart(); |
| 86 | + return (cart?.items?.length ?? 0) === 0; |
| 87 | + } |
| 88 | + |
| 89 | + if (segment === "anonymous-with-cart") { |
| 90 | + if (isLoggedIn) return false; |
| 91 | + const cart = await vtex.loaders.cart(); |
| 92 | + return (cart?.items?.length ?? 0) > 0; |
| 93 | + } |
| 94 | + |
| 95 | + // All remaining segments require login |
| 96 | + if (!isLoggedIn || !payload?.sub) return false; |
| 97 | + |
| 98 | + if (segment === "logged-in") { |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + const email = payload.sub; |
| 103 | + |
| 104 | + if (segment === "logged-in-without-orders") { |
| 105 | + const orders = await vtex.loaders.orders.list({ |
| 106 | + clientEmail: email, |
| 107 | + per_page: "1", |
| 108 | + page: "1", |
| 109 | + }); |
| 110 | + return orders?.paging?.total === 0; |
| 111 | + } |
| 112 | + |
| 113 | + if (segment === "logged-in-with-orders") { |
| 114 | + const orders = await vtex.loaders.orders.list({ |
| 115 | + clientEmail: email, |
| 116 | + per_page: "1", |
| 117 | + page: "1", |
| 118 | + }); |
| 119 | + return orders?.paging?.total > 0; |
| 120 | + } |
| 121 | + |
| 122 | + if (segment === "logged-in-with-recent-orders") { |
| 123 | + const { months = 3 } = props; |
| 124 | + const orders = await vtex.loaders.orders.list({ |
| 125 | + clientEmail: email, |
| 126 | + per_page: "15", |
| 127 | + page: "1", |
| 128 | + }); |
| 129 | + |
| 130 | + if (!orders?.paging?.total) return false; |
| 131 | + |
| 132 | + const now = new Date(); |
| 133 | + const cutoff = new Date(now); |
| 134 | + cutoff.setMonth(cutoff.getMonth() - months); |
| 135 | + |
| 136 | + return orders.list.some((order: { creationDate: string }) => { |
| 137 | + const created = new Date(order.creationDate); |
| 138 | + return created >= cutoff; |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + return false; |
| 143 | + } catch { |
| 144 | + return false; |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +export default MatchUserSegment; |
0 commit comments