From f8967a870ba99d06f9e6505b08dc83930da220e9 Mon Sep 17 00:00:00 2001 From: Paolo Chillari Date: Tue, 1 Feb 2022 14:52:38 +0000 Subject: [PATCH] fix: psa pin request status --- packages/api/src/utils/psa.js | 2 +- packages/api/test/pin.spec.js | 20 ++++++++++++++++++++ packages/db/utils.js | 18 ++++++++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/api/src/utils/psa.js b/packages/api/src/utils/psa.js index ba89a0d9f4..c4b34aa08c 100644 --- a/packages/api/src/utils/psa.js +++ b/packages/api/src/utils/psa.js @@ -3,7 +3,7 @@ */ /** - * @param {import('../../db/db-client-types.js').PinItemOutput[]} pins + * @param {import('@web3-storage/db/db-client-types').PinItemOutput[]} pins * @return {apiPinStatus} status */ export const getEffectivePinStatus = (pins) => { diff --git a/packages/api/test/pin.spec.js b/packages/api/test/pin.spec.js index 6790bcea90..0c8b8f0e10 100644 --- a/packages/api/test/pin.spec.js +++ b/packages/api/test/pin.spec.js @@ -543,6 +543,26 @@ describe('Pinning APIs endpoints', () => { const data = await res.json() assert.strictEqual(data.code, PinningUnauthorizedError.CODE) }) + + it('returns the pin request', async () => { + const sourceCid = 'QmVGv1UK8EvhD9KMHdKBB1LWadiYai6sY5GHL6h7MHRWzf' + const res = await fetch(new URL('pins', endpoint).toString(), { + method: 'POST', + headers: { + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + cid: sourceCid + }) + }) + + assert(res.ok) + const data = await res.json() + assertCorrectPinResponse(data) + assert.strictEqual(data.pin.cid, sourceCid) + assert.notDeepEqual(data.status, 'failed') + }) }) describe('GET /pins/:requestId', () => { diff --git a/packages/db/utils.js b/packages/db/utils.js index a6c87a6bdb..af15855e9a 100644 --- a/packages/db/utils.js +++ b/packages/db/utils.js @@ -11,7 +11,9 @@ export function normalizeUpload (upload) { return { ...nUpload, ...upload.content, - pins: normalizePins(upload.content.pins) + pins: normalizePins(upload.content.pins, { + isOkStatuses: true + }) } } @@ -48,10 +50,18 @@ export function normalizeContent (content) { * Normalize pin items. * * @param {Array} pins + * @param {object} [opt] + * @param {boolean} [opt.isOkStatuses] * @return {Array} */ -export function normalizePins (pins) { - return pins.filter(pin => PIN_STATUS.has(pin.status)) +export function normalizePins (pins, { + isOkStatuses = false +} = {}) { + if (isOkStatuses) { + pins = pins.filter(pin => PIN_OK_STATUS.has(pin.status)) + } + + return pins .map(pin => ({ _id: pin._id, status: pin.status, @@ -85,7 +95,7 @@ export function normalizeDeals (deals) { })) } -const PIN_STATUS = new Set([ +const PIN_OK_STATUS = new Set([ 'Pinned', 'Pinning', 'PinQueued'