diff --git a/packages/api/src/index.js b/packages/api/src/index.js index 30b72faf1f..6c074eb132 100644 --- a/packages/api/src/index.js +++ b/packages/api/src/index.js @@ -98,7 +98,7 @@ r.add('get', '/check/:cid', withMode(nftCheck, RO), [postCors]) r.add('get', '', withMode(nftList, RO), [postCors]) r.add('get', '/:cid', withMode(nftGet, RO), [postCors]) r.add('post', '/upload', withMode(nftUpload, RW), [postCors]) -r.add('patch', '/upload/:id', withMode(nftUpdateUpload, RW), [postCors]) +r.add('patch', '/upload/:cid', withMode(nftUpdateUpload, RW), [postCors]) r.add('post', '/store', withMode(nftStore, RW), [postCors]) r.add('delete', '/:cid', withMode(nftDelete, RW), [postCors]) diff --git a/packages/api/src/routes/nfts-upload.js b/packages/api/src/routes/nfts-upload.js index e2e09b6fd9..d93399f5e0 100644 --- a/packages/api/src/routes/nfts-upload.js +++ b/packages/api/src/routes/nfts-upload.js @@ -172,15 +172,20 @@ export async function nftUpdateUpload(event, ctx) { const { params, db } = ctx try { const { user } = await validate(event, ctx) - const id = params.id + const { cid } = params // id is required for updating - if (!id) return new JSONResponse({ ok: false, value: 'ID is required' }) + if (!cid || typeof cid !== 'string') + return new JSONResponse({ ok: false, value: 'Upload CID is required' }) const body = await event.request.json() const { name } = body - const updatedRecord = await db.updateUpload({ id, name, user_id: user.id }) + const updatedRecord = await db.updateUpload({ + cid, + name, + user_id: user.id, + }) return new JSONResponse({ ok: true, value: updatedRecord }) } catch (/** @type {any} */ err) { diff --git a/packages/api/src/utils/db-client-types.ts b/packages/api/src/utils/db-client-types.ts index 1875356a33..daf0ae4e63 100644 --- a/packages/api/src/utils/db-client-types.ts +++ b/packages/api/src/utils/db-client-types.ts @@ -59,7 +59,7 @@ export interface CreateUploadInput { } export interface UpdateUploadInput { - id: string + cid: string name?: string user_id: number } diff --git a/packages/api/src/utils/db-client.js b/packages/api/src/utils/db-client.js index 8f2606a94f..9df8e72f78 100644 --- a/packages/api/src/utils/db-client.js +++ b/packages/api/src/utils/db-client.js @@ -125,21 +125,17 @@ export class DBClient { updated_at: now, }) .select(this.uploadQuery) - .eq('id', data.id) + .eq('source_cid', data.cid) .eq('user_id', data.user_id) .is('deleted_at', null) .single() if (status === 406) { - throw new Error(`Status 406, cannot update ${data.id}`) + throw new Error(`Status 406, cannot update ${data.cid}`) } if (!upload) { - throw new Error( - `Cannot update upload ${JSON.stringify(data.id)} ${JSON.stringify( - status - )}` - ) + throw new Error(`Cannot update upload ${data.cid} ${status}`) } return upload diff --git a/packages/api/test/nfts-upload.spec.js b/packages/api/test/nfts-upload.spec.js index cc6d5b7906..8118319670 100644 --- a/packages/api/test/nfts-upload.spec.js +++ b/packages/api/test/nfts-upload.spec.js @@ -371,14 +371,14 @@ describe('NFT Upload ', () => { const { data } = await rawClient .from('upload') .select('*') - .match({ source_cid: cid, user_id: client.userId }) + .match({ content_cid: cid, user_id: client.userId }) .single() // update file we just created above const name = 'test updated name' - const uploadRes = await fetch(`upload/${data.id}`, { + const uploadRes = await fetch(`upload/${data.content_cid}`, { method: 'PATCH', headers: { Authorization: `Bearer ${client.token}`,