Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/modules/alerts/__tests__/alert.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { createAlert, listAlerts, deleteAlert } from '../alert.service';
import { prisma } from '../../../utils/prisma.utils';
import { logger } from '../../../utils/logger.utils';

jest.mock('../../../utils/prisma.utils', () => ({
prisma: {
Expand All @@ -17,6 +18,10 @@ jest.mock('../../../utils/prisma.utils', () => ({
},
}));

jest.mock('../../../utils/logger.utils', () => ({
logger: { info: jest.fn() },
}));

const mockedPrisma = prisma as jest.Mocked<typeof prisma>;

const VALID_ADDRESS = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
Expand Down Expand Up @@ -59,6 +64,19 @@ describe('createAlert', () => {
},
});
expect(result).toEqual(DB_ALERT);

expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
alert_id: DB_ALERT.id,
creator_id: DB_ALERT.creatorId,
direction: DB_ALERT.direction,
target_price: DB_ALERT.targetPrice,
registered_at: DB_ALERT.createdAt,
wallet_address: 'GAAA***AAAA',
}),
'Price alert registered'
);
expect((logger.info as jest.Mock).mock.calls[0][0]).not.toHaveProperty('callback_url');
});

it('creates a below-direction alert', async () => {
Expand Down Expand Up @@ -114,6 +132,17 @@ describe('deleteAlert', () => {
where: { id: 'alert-1' },
});
expect(result).toEqual({ id: 'alert-1' });

expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
alert_id: DB_ALERT.id,
creator_id: DB_ALERT.creatorId,
cancelled_at: expect.any(Date),
wallet_address: 'GAAA***AAAA',
}),
'Price alert cancelled'
);
expect((logger.info as jest.Mock).mock.calls[0][0]).not.toHaveProperty('callback_url');
});

it('returns null when the alert is not found', async () => {
Expand Down
32 changes: 31 additions & 1 deletion src/modules/alerts/alert.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type PriceMovement = {
* Creates a new price alert for a wallet address watching a creator's key price.
*/
export async function createAlert(input: CreateAlertInput) {
return await prisma.priceAlert.create({
const alert = await prisma.priceAlert.create({
data: {
creatorId: input.creator_id,
walletAddress: input.wallet_address,
Expand All @@ -22,6 +22,20 @@ export async function createAlert(input: CreateAlertInput) {
callbackUrl: input.callback_url,
},
});

logger.info(
{
alert_id: alert.id,
creator_id: alert.creatorId,
direction: alert.direction,
target_price: toNumber(alert.targetPrice),
registered_at: alert.createdAt,
wallet_address: maskWalletAddress(alert.walletAddress),
},
'Price alert registered'
);

return alert;
}

/**
Expand Down Expand Up @@ -51,13 +65,29 @@ export async function deleteAlert(
}

await prisma.priceAlert.delete({ where: { id } });

logger.info(
{
alert_id: existing.id,
creator_id: existing.creatorId,
cancelled_at: new Date(),
wallet_address: maskWalletAddress(existing.walletAddress),
},
'Price alert cancelled'
);

return { id };
}

function toNumber(value: number | string | { toString(): string }): number {
return typeof value === 'number' ? value : Number(value.toString());
}

function maskWalletAddress(address: string): string {
if (address.length <= 8) return address;
return `${address.slice(0, 4)}***${address.slice(-4)}`;
}

function maskCallbackUrl(callbackUrl: string): string {
try {
const url = new URL(callbackUrl);
Expand Down
Loading