From 0035899809d970cb39fdbb8c3e63ee627da2c200 Mon Sep 17 00:00:00 2001 From: Sanjay Date: Mon, 24 Jul 2023 13:40:36 -0700 Subject: [PATCH] feat: add endpoint to get current proof by name or fid (#46) --- src/app.ts | 20 ++++++++++++++++++++ tests/index.test.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/app.ts b/src/app.ts index ae82fd7c..e8aee38f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,6 +7,7 @@ import { log } from './log.js'; import { generateCCIPSignature, signer, signerAddress } from './signature.js'; import { createTransfer, + getCurrentUsername, getLatestTransfer, getTransferById, getTransferHistory, @@ -63,6 +64,25 @@ app.get('/transfers', async (req, res) => { res.send({ transfers }); }); +app.get('/transfers/current', async (req, res) => { + let name: string | undefined; + if (req.query.fid) { + name = await getCurrentUsername(parseInt(req.query.fid.toString()), db); + } else if (req.query.name) { + name = req.query.name.toString(); + } + if (!name || name === '') { + res.status(404).send({ error: 'Could not resolve current name' }).end(); + return; + } + const transfer = await getLatestTransfer(name, db); + if (!transfer || transfer.to === 0) { + res.status(404).send({ error: 'No transfer found' }).end(); + return; + } + res.send({ transfer }); +}); + app.post('/transfers', async (req, res) => { let tr; try { diff --git a/tests/index.test.ts b/tests/index.test.ts index 0cb52b74..829f7f76 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -80,6 +80,41 @@ describe('app', () => { }); }); + describe('get current transfer', () => { + test('returns error for unknown name', async () => { + let response = await request(app).get('/transfers/current?name=nonexistent'); + expect(response.status).toBe(404); + + // Name was burned + response = await request(app).get('/transfers/current?name=test3'); + expect(response.status).toBe(404); + }); + test('returns error for unknown fid', async () => { + let response = await request(app).get('/transfers/current?fid=129837123'); + expect(response.status).toBe(404); + + // Name was burned + response = await request(app).get('/transfers/current?fid=3'); + expect(response.status).toBe(404); + }); + test('returns error if no name or fid provided', async () => { + const response = await request(app).get('/transfers/current'); + expect(response.status).toBe(404); + }); + test('returns latest transfer for fid', async () => { + await createTestTransfer(db, { username: 'test-current', from: 0, to: 3, timestamp: now + 3 }); + const response = await request(app).get('/transfers/current?fid=3'); + expect(response.status).toBe(200); + expect(response.body.transfer).toMatchObject({ username: 'test-current', from: 0, to: 3, timestamp: now + 3 }); + }); + test('returns latest transfer for name', async () => { + await createTestTransfer(db, { username: 'test3', from: 0, to: 10, timestamp: now + 3 }); + const response = await request(app).get('/transfers/current?name=test3'); + expect(response.status).toBe(200); + expect(response.body.transfer).toMatchObject({ username: 'test3', from: 0, to: 10, timestamp: now + 3 }); + }); + }); + describe('create transfer', () => { const now = currentTimestamp();