Skip to content

Commit

Permalink
fix: don't throw when visit to temp link (#601)
Browse files Browse the repository at this point in the history
Co-authored-by: orig <[email protected]>
  • Loading branch information
origranot and orig authored Dec 11, 2023
1 parent 4d39a73 commit 634e4e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/tracker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"



Expand Down
33 changes: 22 additions & 11 deletions apps/tracker/src/stats/stats.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 634e4e8

Please sign in to comment.