diff --git a/apps/tracker/Dockerfile b/apps/tracker/Dockerfile index bf625dc5..b7694675 100644 --- a/apps/tracker/Dockerfile +++ b/apps/tracker/Dockerfile @@ -37,7 +37,7 @@ COPY --from=build /app/libs/ ./libs EXPOSE 3001 # Start the application -CMD sh -c "npx nx migrate-deploy prisma && node tracker/main.js" +CMD sh -c "node tracker/main.js" diff --git a/apps/tracker/src/stats/stats.service.ts b/apps/tracker/src/stats/stats.service.ts index 32fd7124..56438c21 100644 --- a/apps/tracker/src/stats/stats.service.ts +++ b/apps/tracker/src/stats/stats.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { PrismaService } from '@reduced.to/prisma'; +import { Prisma, PrismaService } from '@reduced.to/prisma'; @Injectable() export class StatsService { @@ -8,18 +8,29 @@ export class StatsService { async addVisit(key: string, opts: { hashedIp: string; ua: string; geo: object }) { const { hashedIp, ua, geo } = opts; - return this.prismaService.visit.create({ - data: { - ip: hashedIp, - userAgent: ua, - ...(geo && { geo }), - link: { - connect: { - key, + try { + await this.prismaService.visit.create({ + data: { + ip: hashedIp, + userAgent: ua, + ...(geo && { geo }), + link: { + connect: { + key, + }, }, }, - }, - }); + }); + } catch (err) { + if (err instanceof Prisma.PrismaClientKnownRequestError) { + if (err.code === 'P2025') { + // Link record does not exist for the given key (might be a visit to a temporary link) + return; + } + + throw err; + } + } } async isUniqueVisit(key: string, hashedIp: string) {