Skip to content

Commit

Permalink
fix: tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
orig committed Dec 15, 2023
1 parent 7319141 commit c0b9941
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/tracker/src/stats/stats.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class StatsConsumer extends ConsumerService {
await this.statsService.addVisit(key, {
hashedIp,
ua: userAgent,
...(geoLocation?.status === 'success' && geoLocation),
...(geoLocation?.status === 'success' && { geoLocation }),
});

message.ack();
Expand Down
13 changes: 8 additions & 5 deletions apps/tracker/src/stats/stats.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ describe('StatsService', () => {
},
};


beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
StatsService,
{
provide: PrismaService,
useValue: mockPrismaService
useValue: mockPrismaService,
},
],
}).compile();
Expand All @@ -35,14 +34,18 @@ describe('StatsService', () => {

describe('addVisit', () => {
it('should handle PrismaClientKnownRequestError', async () => {
const error = new Prisma.PrismaClientKnownRequestError('message', {code: 'P2025', meta: {}, clientVersion: 'v1'});
const error = new Prisma.PrismaClientKnownRequestError('message', { code: 'P2025', meta: {}, clientVersion: 'v1' });
mockPrismaService.visit.create.mockRejectedValue(error);
await expect(service.addVisit('testKey', { hashedIp: 'testIp', ua: 'testAgent' })).resolves.toBeUndefined();
await expect(
service.addVisit('testKey', { hashedIp: 'testIp', ua: 'testAgent', geoLocation: { country: 'United States' } })
).resolves.toBeUndefined();
});

it('should throw an error for unexpected errors', async () => {
mockPrismaService.visit.create.mockRejectedValue(new Error('Unexpected error'));
await expect(service.addVisit('testKey', { hashedIp: 'testIp', ua: 'testAgent' })).rejects.toThrow('Unexpected error');
await expect(
service.addVisit('testKey', { hashedIp: 'testIp', ua: 'testAgent', geoLocation: { country: 'United States' } })
).rejects.toThrow('Unexpected error');
});
});

Expand Down
4 changes: 2 additions & 2 deletions apps/tracker/src/stats/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Prisma, PrismaService } from '@reduced.to/prisma';
export class StatsService {
constructor(private readonly prismaService: PrismaService) {}

async addVisit(key: string, opts: { hashedIp: string; ua: string; geoLocation?: object }) {
async addVisit(key: string, opts: { hashedIp: string; ua: string; geoLocation: object }) {
const { hashedIp, ua, geoLocation } = opts;

try {
await this.prismaService.visit.create({
data: {
ip: hashedIp,
userAgent: ua,
...(geoLocation && { geo: geoLocation }),
geo: geoLocation,
link: {
connect: {
key,
Expand Down

0 comments on commit c0b9941

Please sign in to comment.