Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TPW item upsert support #376

Merged
merged 9 commits into from
Jan 6, 2022
19 changes: 17 additions & 2 deletions spec/mocks/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
ItemRarity,
ItemType,
} from '../../src/Item/Item.types'
import { buildTPItemURN } from '../../src/Item/utils'
import { CollectionAttributes } from '../../src/Collection'
import { collectionAttributesMock } from './collections'

export type ResultItem = Omit<FullItem, 'created_at' | 'updated_at'> & {
Expand All @@ -17,8 +19,15 @@ export type ResultItem = Omit<FullItem, 'created_at' | 'updated_at'> & {
export function toResultItem(
itemAttributes: ItemAttributes,
itemFragment?: ItemFragment,
catalystItem?: Wearable
catalystItem?: Wearable,
dbCollection?: CollectionAttributes
): ResultItem {
const hasURN =
itemAttributes.urn_suffix &&
dbCollection &&
dbCollection.urn_suffix &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use isTPCollection here. Is not necessary, but with the change on the return type here you can remove the ! below later

dbCollection.third_party_id

const resultItem = {
...itemAttributes,
created_at: itemAttributes.created_at.toISOString(),
Expand All @@ -28,7 +37,13 @@ export function toResultItem(
is_published:
Boolean(itemAttributes.collection_id) &&
Boolean(itemAttributes.blockchain_item_id),
urn: null,
urn: hasURN
? buildTPItemURN(
dbCollection!.third_party_id!,
dbCollection!.urn_suffix!,
itemAttributes!.urn_suffix!
)
: null,
total_supply: itemFragment?.totalSupply
? Number(itemFragment?.totalSupply)
: 0,
Expand Down
53 changes: 45 additions & 8 deletions spec/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,53 @@ export function mockCollectionAuthorizationMiddleware(
)
)
if (isThirdParty) {
if (!(thirdPartyAPI.isManager as jest.Mock).mock) {
throw new Error(
"isManager should be mocked to mock the withModelExists middleware but it isn't"
)
}
mockIsThirdPartyManager(ethAddress, isAuthorized)
}
}

/**
* Mocks the "isManager" method of the thirdPartyAPI module.
* This mock requires the thirdPartyAPI's isManager method to be mocked first.
*
* @param ethAddress - The ethAddress of the user that will be requesting authorization to the collection.
* @param isManager - If the user is a manager or not.
*/
export function mockIsThirdPartyManager(
ethAddress: string,
isManager: boolean
) {
if (!(thirdPartyAPI.isManager as jest.Mock).mock) {
throw new Error(
"isManager should be mocked to mock the withModelExists middleware but it isn't"
)
}

;(thirdPartyAPI.isManager as jest.MockedFunction<
typeof thirdPartyAPI.isManager
>).mockImplementationOnce((_, manager) =>
Promise.resolve(manager === ethAddress && isManager)
)
}

;(thirdPartyAPI.isManager as jest.MockedFunction<
typeof thirdPartyAPI.isManager
>).mockResolvedValueOnce(isAuthorized)
/**
* Mocks the "itemExists" method of the thirdPartyAPI module.
* This mock requires the thirdPartyAPI's itemExists method to be mocked first.
*
* @param urn - The URN of the item that will be checked for existence.
* @param exists - If the item with the given URN exists or not.
*/
export function mockThirdPartyItemExists(urn: string, exists: boolean) {
if (!(thirdPartyAPI.itemExists as jest.Mock).mock) {
throw new Error(
"exists should be mocked to mock the withModelExists middleware but it isn't"
)
}

;(thirdPartyAPI.itemExists as jest.MockedFunction<
typeof thirdPartyAPI.itemExists
>).mockImplementationOnce((URNToCheck) =>
Promise.resolve(URNToCheck === urn && exists)
)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class CollectionService {
const collection = await Collection.findOne<CollectionAttributes>(id)

if (collection) {
if (isTPCollection(collection)) {
if (!isTPCollection(collection)) {
throw new WrongCollectionError(
"The collection can't be converted into a third party collection.",
{ id }
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function getThirdPartyCollectionURN(
}

export function isTPCollection(collection: CollectionAttributes): boolean {
return collection.third_party_id === null || collection.urn_suffix === null
return collection.third_party_id !== null && collection.urn_suffix !== null
}

/**
Expand Down
Loading