forked from wangziqi/gongxue-base
feat: improve commerce checkout flow
This commit is contained in:
@@ -45,9 +45,13 @@ const ids = {
|
||||
questionThree: '00000000-0000-0000-0000-000000000405',
|
||||
questionThreeVersion: '00000000-0000-0000-0000-000000000406',
|
||||
plan: '00000000-0000-0000-0000-000000000201',
|
||||
couponOnlyPlan: '00000000-0000-0000-0000-000000000202',
|
||||
checkoutCoupon: '00000000-0000-0000-0000-000000000203',
|
||||
freeCheckoutCoupon: '00000000-0000-0000-0000-000000000204',
|
||||
order: '00000000-0000-0000-0000-000000000701',
|
||||
payment: '00000000-0000-0000-0000-000000000702',
|
||||
activationCode: '00000000-0000-0000-0000-000000000801',
|
||||
selfActivationCode: '00000000-0000-0000-0000-000000000802',
|
||||
vocabularyUnit: '00000000-0000-0000-0000-000000000811',
|
||||
vocabularyWord: '00000000-0000-0000-0000-000000000812',
|
||||
vocabularyWordTwo: '00000000-0000-0000-0000-000000000813',
|
||||
@@ -163,6 +167,93 @@ async function main() {
|
||||
[tenantId, ids.leaderboardSessionUser, ids.leaderboardSessionSecond],
|
||||
);
|
||||
|
||||
const checkoutCouponCodes = ['SMOKE50', 'SMOKEFREE'];
|
||||
|
||||
await client.query(
|
||||
`
|
||||
delete from public.coupon_redemptions
|
||||
where tenant_id = $1
|
||||
and (
|
||||
coupon_id = any($2::uuid[])
|
||||
or coupon_code = any($3::text[])
|
||||
or order_id in (
|
||||
select id from public.orders
|
||||
where tenant_id = $1
|
||||
and (raw_payload->'pricing'->>'couponCode') = any($3::text[])
|
||||
)
|
||||
)
|
||||
`,
|
||||
[tenantId, [ids.checkoutCoupon, ids.freeCheckoutCoupon], checkoutCouponCodes],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
delete from public.order_items
|
||||
where tenant_id = $1
|
||||
and order_id in (
|
||||
select id from public.orders
|
||||
where tenant_id = $1
|
||||
and (raw_payload->'pricing'->>'couponCode') = any($2::text[])
|
||||
)
|
||||
`,
|
||||
[tenantId, checkoutCouponCodes],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
delete from public.entitlements
|
||||
where tenant_id = $1
|
||||
and user_id = $2::uuid
|
||||
and source_type in ('order', 'activation_code')
|
||||
and (
|
||||
legacy_source_id in ('SMOKE20260621', 'SMOKESELF20260621')
|
||||
or source_id in (
|
||||
select id
|
||||
from public.orders
|
||||
where tenant_id = $1
|
||||
and (
|
||||
raw_payload->'pricing'->>'couponCode' = any($3::text[])
|
||||
or order_no like 'SVIP%'
|
||||
or order_no like 'XP%'
|
||||
)
|
||||
)
|
||||
or metadata->>'orderNo' like 'SVIP%'
|
||||
or metadata->>'orderNo' like 'XP%'
|
||||
)
|
||||
`,
|
||||
[tenantId, ids.user, checkoutCouponCodes],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
delete from public.payments
|
||||
where tenant_id = $1
|
||||
and order_id in (
|
||||
select id from public.orders
|
||||
where tenant_id = $1
|
||||
and (
|
||||
(raw_payload->'pricing'->>'couponCode') = any($2::text[])
|
||||
or order_no like 'SVIP%'
|
||||
or order_no like 'XP%'
|
||||
)
|
||||
)
|
||||
`,
|
||||
[tenantId, checkoutCouponCodes],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
delete from public.orders
|
||||
where tenant_id = $1
|
||||
and (
|
||||
(raw_payload->'pricing'->>'couponCode') = any($2::text[])
|
||||
or order_no like 'SVIP%'
|
||||
or order_no like 'XP%'
|
||||
)
|
||||
`,
|
||||
[tenantId, checkoutCouponCodes],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
delete from public.crm_webhook_queue
|
||||
@@ -1193,6 +1284,62 @@ async function main() {
|
||||
[ids.plan, tenantId, ids.region],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
insert into public.svip_plans (
|
||||
id, tenant_id, region_id, legacy_id, name, price_cents, original_price_cents,
|
||||
days, description, per_day_label, badge, recommended, coupon_only, sort_order, is_active
|
||||
)
|
||||
values (
|
||||
$1, $2, $3, 'smoke-coupon-plan', '烟测优惠券专享月卡', 1000, 2000,
|
||||
30, '本地 smoke 优惠券专享套餐', '0.33/天', 'COUPON', false, true, 2, true
|
||||
)
|
||||
on conflict (id)
|
||||
do update set name = excluded.name,
|
||||
price_cents = excluded.price_cents,
|
||||
original_price_cents = excluded.original_price_cents,
|
||||
days = excluded.days,
|
||||
region_id = excluded.region_id,
|
||||
coupon_only = excluded.coupon_only,
|
||||
is_active = true,
|
||||
updated_at = now()
|
||||
`,
|
||||
[ids.couponOnlyPlan, tenantId, ids.region],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
insert into public.coupons (
|
||||
id, tenant_id, legacy_id, code, plan_id, discount_type, discount_value,
|
||||
valid_from, valid_to, max_uses, used_count, source, remark
|
||||
)
|
||||
values
|
||||
(
|
||||
$1, $2, 'smoke-coupon', 'SMOKE50', $3, 'percent', 50,
|
||||
now() - interval '1 day', now() + interval '30 days', 100, 0,
|
||||
'smoke', '本地 smoke 优惠券'
|
||||
),
|
||||
(
|
||||
$4, $2, 'smoke-free-coupon', 'SMOKEFREE', $3, 'percent', 100,
|
||||
now() - interval '1 day', now() + interval '30 days', 100, 0,
|
||||
'smoke', '本地 smoke 全额优惠券'
|
||||
)
|
||||
on conflict (id)
|
||||
do update set code = excluded.code,
|
||||
plan_id = excluded.plan_id,
|
||||
discount_type = excluded.discount_type,
|
||||
discount_value = excluded.discount_value,
|
||||
valid_from = excluded.valid_from,
|
||||
valid_to = excluded.valid_to,
|
||||
max_uses = excluded.max_uses,
|
||||
used_count = 0,
|
||||
source = excluded.source,
|
||||
remark = excluded.remark,
|
||||
updated_at = now()
|
||||
`,
|
||||
[ids.checkoutCoupon, tenantId, ids.couponOnlyPlan, ids.freeCheckoutCoupon],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
insert into public.orders (
|
||||
@@ -1238,6 +1385,24 @@ async function main() {
|
||||
[ids.activationCode, tenantId, ids.region],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
insert into public.activation_codes (
|
||||
id, tenant_id, legacy_id, code, days, is_used, agent_user_id,
|
||||
sale_type, unit_price_cents, used_region_id, remark
|
||||
)
|
||||
values ($1, $2, 'smoke-self-code', 'SMOKESELF20260621', 30, false, $3, 'sales', 0, $4, '不可自用的 smoke 激活码')
|
||||
on conflict (id)
|
||||
do update set is_used = false,
|
||||
used_by = null,
|
||||
used_at = null,
|
||||
agent_user_id = excluded.agent_user_id,
|
||||
used_region_id = excluded.used_region_id,
|
||||
updated_at = now()
|
||||
`,
|
||||
[ids.selfActivationCode, tenantId, ids.user, ids.region],
|
||||
);
|
||||
|
||||
await client.query(
|
||||
`
|
||||
insert into public.vocabulary_units (
|
||||
|
||||
Reference in New Issue
Block a user