231 lines
8.0 KiB
TypeScript
231 lines
8.0 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
import { secondActor, seedProduct } from './helpers/commerce';
|
|
import { checkoutFixture } from './helpers/checkout';
|
|
import { reviewFixture } from './helpers/reviews';
|
|
|
|
describe('live privacy-preserving customer segments', () => {
|
|
let ctx: IdentityApp;
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
await ctx.db.user.updateMany({
|
|
where: {
|
|
organizationId: ctx.owner.organizationId,
|
|
id: { not: ctx.owner.userId },
|
|
},
|
|
data: { status: 'SUSPENDED' },
|
|
});
|
|
});
|
|
const members = (key: string, token = ctx.token, query: object = {}) =>
|
|
ctx
|
|
.api()
|
|
.get('/api/v1/customer-segments/' + key + '/members')
|
|
.auth(token, { type: 'bearer' })
|
|
.query(query);
|
|
const save = (token: string, variantId: string) =>
|
|
ctx
|
|
.api()
|
|
.put('/api/v1/wishlist/' + variantId)
|
|
.auth(token, { type: 'bearer' })
|
|
.send({})
|
|
.expect(200);
|
|
it('deduplicates current wishlist interest and excludes unavailable and inactive accounts', async () => {
|
|
const actor = await secondActor(ctx);
|
|
const a = await seedProduct(ctx, true);
|
|
const b = await seedProduct(ctx, true);
|
|
await save(actor.token, a.variant.id);
|
|
await save(actor.token, b.variant.id);
|
|
const result = await members('WISHLIST_INTEREST').expect(200);
|
|
expect(result.body).toMatchObject({
|
|
segment: 'WISHLIST_INTEREST',
|
|
total: 1,
|
|
members: [{ userId: actor.userId }],
|
|
});
|
|
expect(JSON.stringify(result.body)).not.toMatch(
|
|
/email|name|phone|password|address|note|reference/,
|
|
);
|
|
await ctx.db.productVariant.update({
|
|
where: { id: a.variant.id },
|
|
data: { active: false },
|
|
});
|
|
expect((await members('WISHLIST_INTEREST').expect(200)).body.total).toBe(1);
|
|
await ctx.db.product.update({
|
|
where: { id: b.product.id },
|
|
data: { status: 'DRAFT' },
|
|
});
|
|
expect((await members('WISHLIST_INTEREST').expect(200)).body.total).toBe(0);
|
|
await ctx.db.productVariant.update({
|
|
where: { id: a.variant.id },
|
|
data: { active: true },
|
|
});
|
|
await ctx.db.user.update({
|
|
where: { id: actor.userId },
|
|
data: { status: 'SUSPENDED' },
|
|
});
|
|
expect((await members('WISHLIST_INTEREST').expect(200)).body.total).toBe(0);
|
|
});
|
|
it('shares cart recovery timing, excludes empty carts and reacts immediately to edits', async () => {
|
|
const f = await checkoutFixture(ctx);
|
|
expect((await members('RECOVERABLE_CART').expect(200)).body.total).toBe(0);
|
|
await ctx.db.cart.updateMany({
|
|
where: { userId: f.actor.userId },
|
|
data: { lastActivityAt: new Date(Date.now() - 25 * 3600000) },
|
|
});
|
|
expect(
|
|
(await members('RECOVERABLE_CART').expect(200)).body.members,
|
|
).toEqual([{ userId: f.actor.userId }]);
|
|
await ctx
|
|
.api()
|
|
.put('/api/v1/cart/lines/' + f.variant.id)
|
|
.auth(f.actor.token, { type: 'bearer' })
|
|
.send({ version: 1, quantity: 3 })
|
|
.expect(200);
|
|
expect((await members('RECOVERABLE_CART').expect(200)).body.total).toBe(0);
|
|
await ctx.db.cart.updateMany({
|
|
where: { userId: f.actor.userId },
|
|
data: { lastActivityAt: null },
|
|
});
|
|
expect((await members('RECOVERABLE_CART').expect(200)).body.total).toBe(0);
|
|
await ctx.db.cart.updateMany({
|
|
where: { userId: f.actor.userId },
|
|
data: { lastActivityAt: new Date(Date.now() - 25 * 3600000) },
|
|
});
|
|
await ctx
|
|
.api()
|
|
.delete('/api/v1/cart/lines/' + f.variant.id)
|
|
.auth(f.actor.token, { type: 'bearer' })
|
|
.send({ version: 2 })
|
|
.expect(200);
|
|
await ctx.db.cart.updateMany({
|
|
where: { userId: f.actor.userId },
|
|
data: { lastActivityAt: new Date(Date.now() - 25 * 3600000) },
|
|
});
|
|
expect((await members('RECOVERABLE_CART').expect(200)).body.total).toBe(0);
|
|
});
|
|
it('uses external verified evidence and removes revoked buyers without counting unpaid orders', async () => {
|
|
const f = await reviewFixture(ctx);
|
|
const checkout = await checkoutFixture(ctx);
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/checkout')
|
|
.auth(checkout.actor.token, { type: 'bearer' })
|
|
.send(checkout.input)
|
|
.expect(201);
|
|
expect((await members('VERIFIED_BUYER').expect(200)).body.members).toEqual([
|
|
{ userId: f.buyer.userId },
|
|
]);
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/review-verifications/' + f.proof.id + '/revoke')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ reason: 'Evidence invalidated', idempotencyKey: randomUUID() })
|
|
.expect(201);
|
|
expect((await members('VERIFIED_BUYER').expect(200)).body.total).toBe(0);
|
|
});
|
|
it('uses the latest channel/purpose consent and requires its separate read permission', async () => {
|
|
const actor = await secondActor(ctx);
|
|
const item = await seedProduct(ctx, true);
|
|
await save(actor.token, item.variant.id);
|
|
const staff = await secondActor(ctx, true, ['segments.read']);
|
|
const filter = { channel: 'EMAIL', purpose: 'PROMOTIONS' };
|
|
await members('WISHLIST_INTEREST', staff.token).expect(200);
|
|
await members('WISHLIST_INTEREST', staff.token, filter).expect(403);
|
|
expect(
|
|
(await members('WISHLIST_INTEREST', ctx.token, filter).expect(200)).body
|
|
.total,
|
|
).toBe(0);
|
|
const input = {
|
|
...filter,
|
|
version: 0,
|
|
granted: true,
|
|
noticeVersion: '2026-10-07',
|
|
idempotencyKey: randomUUID(),
|
|
};
|
|
const consent = (body: object) =>
|
|
ctx
|
|
.api()
|
|
.post('/api/v1/messaging/preferences')
|
|
.auth(actor.token, { type: 'bearer' })
|
|
.send(body)
|
|
.expect(201);
|
|
await consent(input);
|
|
expect(
|
|
(await members('WISHLIST_INTEREST', ctx.token, filter).expect(200)).body
|
|
.total,
|
|
).toBe(1);
|
|
expect(
|
|
(
|
|
await members('WISHLIST_INTEREST', ctx.token, {
|
|
channel: 'WHATSAPP',
|
|
purpose: 'PROMOTIONS',
|
|
}).expect(200)
|
|
).body.total,
|
|
).toBe(0);
|
|
expect(
|
|
(
|
|
await members('WISHLIST_INTEREST', ctx.token, {
|
|
channel: 'EMAIL',
|
|
purpose: 'CART_RECOVERY',
|
|
}).expect(200)
|
|
).body.total,
|
|
).toBe(0);
|
|
await consent({
|
|
...filter,
|
|
version: 1,
|
|
granted: false,
|
|
idempotencyKey: randomUUID(),
|
|
});
|
|
await consent(input);
|
|
expect(
|
|
(await members('WISHLIST_INTEREST', ctx.token, filter).expect(200)).body
|
|
.total,
|
|
).toBe(0);
|
|
});
|
|
it('isolates organization audiences, paginates IDs and validates all filter inputs', async () => {
|
|
const first = await secondActor(ctx);
|
|
const second = await secondActor(ctx);
|
|
const item = await seedProduct(ctx, true);
|
|
await save(first.token, item.variant.id);
|
|
await save(second.token, item.variant.id);
|
|
const result = await members('WISHLIST_INTEREST', ctx.token, {
|
|
limit: 1,
|
|
offset: 1,
|
|
}).expect(200);
|
|
expect(result.body.total).toBe(2);
|
|
expect(result.body.members).toHaveLength(1);
|
|
const foreign = await secondActor(ctx, false, ['segments.read']);
|
|
expect(
|
|
(await members('WISHLIST_INTEREST', foreign.token).expect(200)).body
|
|
.total,
|
|
).toBe(0);
|
|
await members('WISHLIST_INTEREST', first.token).expect(403);
|
|
await members('UNKNOWN').expect(400);
|
|
for (const query of [
|
|
{ limit: 101 },
|
|
{ offset: -1 },
|
|
{ channel: 'EMAIL' },
|
|
{ purpose: 'PROMOTIONS' },
|
|
{ channel: 'SMS', purpose: 'PROMOTIONS' },
|
|
{ organizationId: foreign.organizationId },
|
|
])
|
|
await members('WISHLIST_INTEREST', ctx.token, query).expect(400);
|
|
await ctx.api().get('/api/v1/customer-segments').expect(401);
|
|
const summary = await ctx
|
|
.api()
|
|
.get('/api/v1/customer-segments')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.expect(200);
|
|
expect(summary.body.segments).toHaveLength(3);
|
|
expect(
|
|
summary.body.segments.find((s: any) => s.key === 'WISHLIST_INTEREST')
|
|
.total,
|
|
).toBe(2);
|
|
});
|
|
});
|