This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { createHash } from 'node:crypto'
|
||||
import { prisma } from './db.js'
|
||||
|
||||
interface DemoAddress {
|
||||
name: string
|
||||
address: string
|
||||
latitude: number
|
||||
longitude: number
|
||||
countryCode: string
|
||||
}
|
||||
|
||||
const demoAddresses: DemoAddress[] = [
|
||||
{
|
||||
name: 'Almaty warehouse',
|
||||
address: 'Tole Bi Street 101, Almaty',
|
||||
latitude: 43.2527,
|
||||
longitude: 76.9278,
|
||||
countryCode: 'KZ',
|
||||
},
|
||||
{
|
||||
name: 'Moscow office',
|
||||
address: 'Presnenskaya Embankment 8, Moscow',
|
||||
latitude: 55.7472,
|
||||
longitude: 37.5391,
|
||||
countryCode: 'RU',
|
||||
},
|
||||
{
|
||||
name: 'Dubai warehouse',
|
||||
address: 'Dubai South Logistics District, Dubai',
|
||||
latitude: 24.8964,
|
||||
longitude: 55.1614,
|
||||
countryCode: 'AE',
|
||||
},
|
||||
]
|
||||
|
||||
function stableUuid(teamUuid: string, index: number): string {
|
||||
const hex = createHash('sha256')
|
||||
.update(`${teamUuid}:demo-address:${index}`)
|
||||
.digest('hex')
|
||||
.slice(0, 32)
|
||||
.split('')
|
||||
hex[12] = '4'
|
||||
hex[16] = '8'
|
||||
const value = hex.join('')
|
||||
return `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`
|
||||
}
|
||||
|
||||
export async function ensureDemoTeamAddresses(teamId: number, teamUuid: string): Promise<void> {
|
||||
const addressCount = await prisma.teamAddress.count({ where: { teamId } })
|
||||
if (addressCount > 0) return
|
||||
|
||||
for (const [index, address] of demoAddresses.entries()) {
|
||||
await prisma.teamAddress.upsert({
|
||||
where: { uuid: stableUuid(teamUuid, index) },
|
||||
create: {
|
||||
uuid: stableUuid(teamUuid, index),
|
||||
teamId,
|
||||
...address,
|
||||
isDefault: index === 0,
|
||||
status: 'ready',
|
||||
processedAt: new Date(),
|
||||
},
|
||||
update: {
|
||||
teamId,
|
||||
...address,
|
||||
isDefault: index === 0,
|
||||
status: 'ready',
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { GraphQLError } from 'graphql'
|
||||
import { randomUUID, createHash } from 'crypto'
|
||||
import { prisma } from '../db.js'
|
||||
import { requireScopes, type AuthContext } from '../auth.js'
|
||||
import { ensureDemoTeamAddresses } from '../demo-addresses.js'
|
||||
import { materializeTeamFromContext } from '../team-materialization.js'
|
||||
|
||||
export const teamTypeDefs = `#graphql
|
||||
@@ -155,6 +156,8 @@ export const teamResolvers = {
|
||||
requireScopes(ctx, 'teams:member')
|
||||
if (!ctx.teamUuid) throw new GraphQLError('Team UUID not found')
|
||||
await materializeTeamFromContext(ctx)
|
||||
const materializedTeam = await prisma.team.findUnique({ where: { uuid: ctx.teamUuid } })
|
||||
if (materializedTeam) await ensureDemoTeamAddresses(materializedTeam.id, ctx.teamUuid)
|
||||
const team = await getTeamByUuid(ctx.teamUuid)
|
||||
return team ? mapTeam(team) : null
|
||||
},
|
||||
@@ -192,6 +195,7 @@ export const teamResolvers = {
|
||||
await materializeTeamFromContext(ctx)
|
||||
const team = await prisma.team.findUnique({ where: { uuid: ctx.teamUuid } })
|
||||
if (!team) return []
|
||||
await ensureDemoTeamAddresses(team.id, ctx.teamUuid)
|
||||
const addrs = await prisma.teamAddress.findMany({ where: { teamId: team.id }, orderBy: { createdAt: 'desc' } })
|
||||
return addrs.map(a => ({
|
||||
uuid: a.uuid, name: a.name, address: a.address,
|
||||
|
||||
Reference in New Issue
Block a user