114 lines
3.2 KiB
TypeScript
114 lines
3.2 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import type { IdentityApp } from './identity-app';
|
|
import { issueToken } from '../../src/identity/tokens';
|
|
|
|
export const variantInput = {
|
|
sku: 'CANDLE',
|
|
name: 'Rose 200g',
|
|
price: '499.00',
|
|
currency: 'INR' as const,
|
|
active: true,
|
|
attributes: { fragrance: 'rose' },
|
|
};
|
|
export const addressInput = {
|
|
recipient: 'Test Customer',
|
|
line1: '12 Test Road',
|
|
line2: '',
|
|
city: 'Pune',
|
|
region: 'Maharashtra',
|
|
postalCode: '411001',
|
|
countryCode: 'IN',
|
|
phone: '+919876543210',
|
|
isDefault: false,
|
|
};
|
|
export async function seedProduct(ctx: IdentityApp, publish = false) {
|
|
const slug = 'candle-' + randomUUID();
|
|
const product = await ctx
|
|
.api()
|
|
.post('/api/v1/products')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ name: 'Rose Candle', slug, description: 'Handmade candle' })
|
|
.expect(201);
|
|
const variant = await ctx
|
|
.api()
|
|
.post(`/api/v1/products/${product.body.id}/variants`)
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ ...variantInput, sku: randomUUID() })
|
|
.expect(201);
|
|
if (publish)
|
|
await ctx
|
|
.api()
|
|
.patch(`/api/v1/products/${product.body.id}/status`)
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ status: 'PUBLISHED' })
|
|
.expect(200);
|
|
return { product: product.body, variant: variant.body };
|
|
}
|
|
export async function seedStock(ctx: IdentityApp, onHand = 10) {
|
|
const catalog = await seedProduct(ctx, true);
|
|
const warehouse = await ctx
|
|
.api()
|
|
.post('/api/v1/inventory/warehouses')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ name: randomUUID() })
|
|
.expect(201);
|
|
const stock = await ctx
|
|
.api()
|
|
.post('/api/v1/inventory/stock-items')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({ warehouseId: warehouse.body.id, variantId: catalog.variant.id })
|
|
.expect(201);
|
|
if (onHand)
|
|
await ctx
|
|
.api()
|
|
.post('/api/v1/inventory/adjustments')
|
|
.auth(ctx.token, { type: 'bearer' })
|
|
.send({
|
|
stockItemId: stock.body.id,
|
|
delta: onHand,
|
|
reason: 'Opening balance',
|
|
idempotencyKey: randomUUID(),
|
|
})
|
|
.expect(201);
|
|
return { ...catalog, warehouse: warehouse.body, stock: stock.body };
|
|
}
|
|
export async function secondActor(
|
|
ctx: IdentityApp,
|
|
sameOrganization = true,
|
|
permissions: string[] = [],
|
|
) {
|
|
const organizationId = sameOrganization
|
|
? ctx.owner.organizationId
|
|
: (
|
|
await ctx.db.organization.create({
|
|
data: { name: 'Other organization' },
|
|
})
|
|
).id;
|
|
const user = await ctx.db.user.create({
|
|
data: {
|
|
organizationId,
|
|
email: randomUUID() + '@example.com',
|
|
name: 'Test account',
|
|
passwordHash: 'not-a-login-credential',
|
|
status: 'ACTIVE',
|
|
},
|
|
});
|
|
if (permissions.length) {
|
|
const role = await ctx.db.role.create({
|
|
data: { organizationId, name: randomUUID(), permissions },
|
|
});
|
|
await ctx.db.userRole.create({
|
|
data: { organizationId, userId: user.id, roleId: role.id },
|
|
});
|
|
}
|
|
const token = issueToken();
|
|
await ctx.db.session.create({
|
|
data: {
|
|
userId: user.id,
|
|
tokenHash: token.tokenHash,
|
|
expiresAt: new Date(Date.now() + 60000),
|
|
},
|
|
});
|
|
return { token: token.token, userId: user.id, organizationId };
|
|
}
|