diff --git a/packages/api/test/mocks/pgrest/post_rpc#find_deals_by_content_cids.js b/packages/api/test/mocks/pgrest/post_rpc#find_deals_by_content_cids.js index 45e9957339..ef0cfa3a98 100644 --- a/packages/api/test/mocks/pgrest/post_rpc#find_deals_by_content_cids.js +++ b/packages/api/test/mocks/pgrest/post_rpc#find_deals_by_content_cids.js @@ -5,17 +5,30 @@ module.exports = ({ body }) => { // main cid if (body.cids.includes('bafybeifnfkzjeohjf2dch2iqqpef3bfjylwxlcjws2msvdfyze5bvdprfm')) { - return [{ - dealId: 12345, - storageProvider: 'f99', - status: 'Active', - pieceCid: 'baga', - dataCid: 'bafybeifnfkzjeohjf2dch2iqqpef3bfjylwxlcjws2msvdfyze5bvdprfm', - dataModelSelector: 'Links/0/Links', - activation: '', - created: '2021-07-14T19:27:14.934572Z', - updated: '2021-07-14T19:27:14.934572Z' - }] + return [ + { + dealId: 12345, + storageProvider: 'f99', + status: 'Active', + pieceCid: 'baga', + dataCid: 'bafybeifnfkzjeohjf2dch2iqqpef3bfjylwxlcjws2msvdfyze5bvdprfm', + dataModelSelector: 'Links/0/Links', + dealActivation: '', + created: '2021-07-14T19:27:14.934572Z', + updated: '2021-07-14T19:27:14.934572Z' + }, + { + dealId: 123456, + storageProvider: 'f98', + status: 'Terminated', + pieceCid: 'baga', + dataCid: 'bafybeifnfkzjeohjf2dch2iqqpef3bfjylwxlcjws2msvdfyze5bvdprfm', + dataModelSelector: 'Links/0/Links', + dealActivation: '', + created: '2021-07-14T19:27:14.934572Z', + updated: '2021-07-14T19:27:14.934572Z' + } + ] } else if (body.cids.includes('bafybeica6klnrhlrbx6z24icefykpbwyypouglnypvnwb5esdm6yzcie3q')) { return [{ status: 'Queued', diff --git a/packages/db/index.js b/packages/db/index.js index 8b60f2f5ce..4f6225f698 100644 --- a/packages/db/index.js +++ b/packages/db/index.js @@ -1,6 +1,6 @@ import { PostgrestClient } from '@supabase/postgrest-js' -import { normalizeUpload, normalizeContent, normalizePins } from './utils.js' +import { normalizeUpload, normalizeContent, normalizePins, normalizeDeals } from './utils.js' import { DBError } from './errors.js' import { getUserMetrics, @@ -566,9 +566,8 @@ export class DBClient { throw new DBError(error) } - // TODO: normalize deal by removing deal prefix on dealActivation and dealExpiration const result = {} - for (const d of data) { + for (const d of normalizeDeals(data)) { const cid = d.dataCid if (!Array.isArray(result[cid])) { result[cid] = [d] diff --git a/packages/db/utils.js b/packages/db/utils.js index c985c79f55..a12f35f5a3 100644 --- a/packages/db/utils.js +++ b/packages/db/utils.js @@ -35,15 +35,35 @@ export function normalizeContent (content) { * @return {Array} */ export function normalizePins (pins) { - return pins.map(pin => ({ - _id: pin._id, - status: pin.status, - created: pin.created, - updated: pin.updated, - peerId: pin.location.peerId, - peerName: pin.location.peerName, - region: pin.location.region - })).filter(pin => PIN_STATUS.has(pin.status)) + return pins.filter(pin => PIN_STATUS.has(pin.status)) + .map(pin => ({ + _id: pin._id, + status: pin.status, + created: pin.created, + updated: pin.updated, + peerId: pin.location.peerId, + peerName: pin.location.peerName, + region: pin.location.region + })) +} + +/** + * Normalize deal items. + */ +export function normalizeDeals (deals) { + return deals.filter(deal => DEAL_STATUS.has(deal.status)) + .map(deal => ({ + dealId: deal.dealId, + storageProvider: deal.storageProvider, + status: deal.status, + pieceCid: deal.pieceCid, + dataCid: deal.dataCid, + dataModelSelector: deal.dataModelSelector, + activation: deal.dealActivation, + expiration: deal.dealExpiration, + created: deal.created, + updated: deal.updated + })) } const PIN_STATUS = new Set([ @@ -51,3 +71,9 @@ const PIN_STATUS = new Set([ 'Pinning', 'PinQueued' ]) + +const DEAL_STATUS = new Set([ + 'Queued', + 'Published', + 'Active' +])