import { prisma } from '../dist/db.js' const DEMO_TEAM_UUID = process.env.DEMO_TEAM_UUID ?? '11111111-1111-4111-8111-111111111111' const DEMO_TEAM_NAME = process.env.DEMO_TEAM_NAME ?? 'Optovia Demo Client' const DEMO_ADDRESS_COUNT = Number(process.env.DEMO_ADDRESS_COUNT ?? '3') function requiredEnv(name: string): string { const value = process.env[name]?.trim() if (!value) throw new Error(`${name} is required`) return value } function demoAddress(index: number) { const row = Math.floor(index / 60) const col = index % 60 const latitude = 43.238949 + row * 0.018 + (col % 5) * 0.002 const longitude = 76.889709 + col * 0.021 + (row % 5) * 0.002 return { uuid: `11111111-2222-4${String(index).padStart(3, '0').slice(-3)}-8${String(index).padStart(3, '0').slice(-3)}-111111${String(index).padStart(6, '0')}`, name: `Demo address ${index + 1}`, address: `Warehouse ${index + 1}, Almaty logistics area`, latitude, longitude, countryCode: 'KZ', isDefault: index === 0, status: 'processed', processedAt: new Date(), } } const logtoId = requiredEnv('DEMO_LOGTO_USER_ID') const email = process.env.DEMO_USER_EMAIL ?? 'demo@optovia.ru' const phone = process.env.DEMO_USER_PHONE ?? '+79990000000' const firstName = process.env.DEMO_USER_FIRST_NAME ?? 'Demo' const lastName = process.env.DEMO_USER_LAST_NAME ?? 'Client' let profile = await prisma.userProfile.findUnique({ where: { logtoId }, include: { user: true }, }) if (profile === null) { const user = await prisma.user.upsert({ where: { username: logtoId }, create: { username: logtoId, firstName, lastName, email }, update: { firstName, lastName, email, isActive: true }, }) profile = await prisma.userProfile.create({ data: { userId: user.id, logtoId, phone, avatarId: 'demo-client' }, include: { user: true }, }) } else { await prisma.user.update({ where: { id: profile.userId }, data: { firstName, lastName, email, isActive: true }, }) profile = await prisma.userProfile.update({ where: { id: profile.id }, data: { phone, avatarId: 'demo-client' }, include: { user: true }, }) } const team = await prisma.team.upsert({ where: { uuid: DEMO_TEAM_UUID }, create: { uuid: DEMO_TEAM_UUID, name: DEMO_TEAM_NAME, teamType: 'BUYER', ownerId: profile.userId, selectedLocationType: 'ADDRESS', selectedLocationUuid: '11111111-2222-4000-8000-111111000000', selectedLocationName: 'Demo address 1', selectedLocationLat: 43.238949, selectedLocationLon: 76.889709, }, update: { name: DEMO_TEAM_NAME, teamType: 'BUYER', ownerId: profile.userId, selectedLocationType: 'ADDRESS', selectedLocationUuid: '11111111-2222-4000-8000-111111000000', selectedLocationName: 'Demo address 1', selectedLocationLat: 43.238949, selectedLocationLon: 76.889709, }, }) await prisma.teamMember.upsert({ where: { teamId_userId: { teamId: team.id, userId: profile.userId, }, }, create: { teamId: team.id, userId: profile.userId, role: 'OWNER' }, update: { role: 'OWNER' }, }) await prisma.userProfile.update({ where: { id: profile.id }, data: { activeTeamId: team.id }, }) const addresses = Array.from({ length: DEMO_ADDRESS_COUNT }, (_, index) => ({ ...demoAddress(index), teamId: team.id, })) const addressUuids = addresses.map((address) => address.uuid) await prisma.teamAddress.deleteMany({ where: { teamId: team.id, name: { startsWith: 'Demo address ' }, uuid: { notIn: addressUuids }, }, }) await prisma.teamAddress.createMany({ data: addresses, skipDuplicates: true, }) await prisma.teamAddress.updateMany({ where: { teamId: team.id }, data: { isDefault: false }, }) await prisma.teamAddress.update({ where: { uuid: '11111111-2222-4000-8000-111111000000' }, data: { isDefault: true }, }) console.log(`Seeded teams demo data for ${logtoId}: team ${DEMO_TEAM_UUID}, addresses ${DEMO_ADDRESS_COUNT}`) await prisma.$disconnect()