Skip to content

Commit

Permalink
fix: psa pin request status
Browse files Browse the repository at this point in the history
  • Loading branch information
flea89 authored Feb 1, 2022
1 parent f1e9da5 commit f8967a8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/utils/psa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
20 changes: 20 additions & 0 deletions packages/api/test/pin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
18 changes: 14 additions & 4 deletions packages/db/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export function normalizeUpload (upload) {
return {
...nUpload,
...upload.content,
pins: normalizePins(upload.content.pins)
pins: normalizePins(upload.content.pins, {
isOkStatuses: true
})
}
}

Expand Down Expand Up @@ -48,10 +50,18 @@ export function normalizeContent (content) {
* Normalize pin items.
*
* @param {Array<import('./db-client-types').PinItem>} pins
* @param {object} [opt]
* @param {boolean} [opt.isOkStatuses]
* @return {Array<import('./db-client-types').PinItemOutput>}
*/
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,
Expand Down Expand Up @@ -85,7 +95,7 @@ export function normalizeDeals (deals) {
}))
}

const PIN_STATUS = new Set([
const PIN_OK_STATUS = new Set([
'Pinned',
'Pinning',
'PinQueued'
Expand Down

0 comments on commit f8967a8

Please sign in to comment.