Skip to content

Commit

Permalink
Remove collection util file to only have utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Oct 15, 2021
1 parent 58655e9 commit 230e792
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 76 deletions.
68 changes: 0 additions & 68 deletions src/Collection/util.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Collection } from '.'
import { Bridge } from '../ethereum/api/Bridge'
import { collectionAPI } from '../ethereum/api/collection'
import { getMergedCollection } from './util'
import { getMergedCollection } from './utils'

describe('getMergedCollection', () => {
let sampleCollection: { id: string }
Expand Down
69 changes: 68 additions & 1 deletion src/Collection/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { utils } from 'decentraland-commons'
import { getCurrentNetworkURNProtocol } from '../ethereum/utils'
import { CollectionAttributes, FullCollection } from './Collection.types'
import { Collection, CollectionAttributes, FullCollection } from '.'
import { Bridge } from '../ethereum/api/Bridge'
import { collectionAPI } from '../ethereum/api/collection'
import { CollectionFragment } from '../ethereum/api/fragments'

export function getDecentralandCollectionURN(
collectionAddress: string
Expand All @@ -25,3 +28,67 @@ export function toDBCollection(
urn_suffix: null,
}
}

type GetMergedCollectionResult =
| { status: 'not_found'; collection: undefined }
| { status: 'incomplete' | 'complete'; collection: CollectionAttributes }

/**
* Will return a collection formed by merging the collection present in
* the database and the one found in the graph.
*
* The result will depend according to the availability of those collections.
*
* When the collection does not exist on the database:
* status = not_found
*
* When the collection does not exist on the graph:
* status = incomplete & collection = database collection
*
* When both collections are available:
* status = complete & collection = merged db and graph collection
*/
export const getMergedCollection = async (
id: string
): Promise<GetMergedCollectionResult> => {
const dbCollection = await getDBCollection(id)

if (!dbCollection) {
return {
status: 'not_found',
collection: undefined,
}
}

const remoteCollection = await getRemoteCollection(
dbCollection.contract_address
)

if (!remoteCollection) {
return {
status: 'incomplete',
collection: dbCollection,
}
}

const mergedCollection = mergeCollections(dbCollection, remoteCollection)

return {
status: 'complete',
collection: mergedCollection,
}
}

export const getDBCollection = (id: string) =>
Collection.findOne<CollectionAttributes>(id)

export const getRemoteCollection = async (contractAddress: string) =>
(await collectionAPI.fetchCollection(contractAddress)) || undefined

export const getRemoteCollections = async () =>
await collectionAPI.fetchCollections()

export const mergeCollections = (
db: CollectionAttributes,
remote: CollectionFragment
) => Bridge.mergeCollection(db, remote)
4 changes: 2 additions & 2 deletions src/Curation/Curation.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Curation, CurationRouter } from '.'
import { ExpressApp } from '../common/ExpressApp'
import { isCommitteeMember } from '../Committee'
import { hasAccessToCollection } from './access'
import { getMergedCollection } from '../Collection/util'
import { getMergedCollection } from '../Collection/utils'
import { collectionAPI } from '../ethereum/api/collection'
import { Collection } from '../Collection'

jest.mock('../common/Router')
jest.mock('../common/ExpressApp')
jest.mock('../Committee')
jest.mock('../Collection/util')
jest.mock('../Collection/utils')
jest.mock('./access')

const mockIsComiteeMember = isCommitteeMember as jest.Mock
Expand Down
2 changes: 1 addition & 1 deletion src/Curation/Curation.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HTTPError, STATUS_CODES } from '../common/HTTPError'
import { withAuthentication, AuthRequest } from '../middleware'
import { Curation, patchCurationSchema } from '.'
import { hasAccessToCollection } from './access'
import { getMergedCollection } from '../Collection/util'
import { getMergedCollection } from '../Collection/utils'
import { isCommitteeMember } from '../Committee'
import { collectionAPI } from '../ethereum/api/collection'
import { Collection } from '../Collection'
Expand Down
4 changes: 2 additions & 2 deletions src/Curation/access.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getMergedCollection } from '../Collection/util'
import { getMergedCollection } from '../Collection/utils'
import { isCommitteeMember } from '../Committee'
import { Ownable } from '../Ownable'
import { hasAccessToCollection } from './access'

jest.mock('../Committee')
jest.mock('../Collection/util')
jest.mock('../Collection/utils')

const mockIsCommitteeMember = isCommitteeMember as jest.Mock
const isOwnedBySpy = jest.spyOn(Ownable.prototype, 'isOwnedBy')
Expand Down
2 changes: 1 addition & 1 deletion src/Curation/access.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isCommitteeMember } from '../Committee'
import { Ownable } from '../Ownable'
import { Collection, CollectionAttributes } from '../Collection'
import { getMergedCollection } from '../Collection/util'
import { getMergedCollection } from '../Collection/utils'

export async function hasAccessToCollection(
ethAddress: string,
Expand Down

0 comments on commit 230e792

Please sign in to comment.