Skip to content

Commit

Permalink
fix: deal normalize should filter out unexpected deal status (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Nov 23, 2021
1 parent fb76d07 commit 0e233cc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<iso timestamp>',
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: '<iso timestamp>',
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: '<iso timestamp>',
created: '2021-07-14T19:27:14.934572Z',
updated: '2021-07-14T19:27:14.934572Z'
}
]
} else if (body.cids.includes('bafybeica6klnrhlrbx6z24icefykpbwyypouglnypvnwb5esdm6yzcie3q')) {
return [{
status: 'Queued',
Expand Down
5 changes: 2 additions & 3 deletions packages/db/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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]
Expand Down
44 changes: 35 additions & 9 deletions packages/db/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,45 @@ export function normalizeContent (content) {
* @return {Array<import('../db-client-types').PinItemOutput>}
*/
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([
'Pinned',
'Pinning',
'PinQueued'
])

const DEAL_STATUS = new Set([
'Queued',
'Published',
'Active'
])

0 comments on commit 0e233cc

Please sign in to comment.