Skip to content

Commit

Permalink
feat: Add assignee column to collection curations (#506)
Browse files Browse the repository at this point in the history
* feat: Add assignee column to collection curations

* fix: update test
  • Loading branch information
juanmahidalgo committed Apr 26, 2022
1 parent 5556875 commit d56747e
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 40 deletions.
15 changes: 15 additions & 0 deletions migrations/1650963111605_add-assignee-to-curations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationBuilder } from 'node-pg-migrate'
import { CollectionCuration } from '../src/Curation/CollectionCuration'

const tableName = CollectionCuration.tableName
const columnName = 'assignee'

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.addColumn(tableName, {
[columnName]: { type: 'TEXT' },
})
}

export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.dropColumn(tableName, columnName)
}
1 change: 1 addition & 0 deletions src/Collection/Collection.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,7 @@ describe('Collection router', () => {
status: CurationStatus.PENDING,
created_at: expect.any(Date),
updated_at: expect.any(Date),
assignee: null,
})
})
})
Expand Down
1 change: 1 addition & 0 deletions src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export class CollectionService {
status: CurationStatus.PENDING,
created_at: now,
updated_at: now,
assignee: null,
})
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CurationStatus } from '../Curation.types'
export type CollectionCurationAttributes = {
id: string
collection_id: string
assignee: string | null
status: CurationStatus
created_at: Date
updated_at: Date
Expand Down
181 changes: 145 additions & 36 deletions src/Curation/Curation.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jest.mock('../Curation/ItemCuration')

const mockIsCommitteeMember = isCommitteeMember as jest.Mock

const mockAddress = '0x6D7227d6F36FC997D53B4646132b3B55D751cc7c'

describe('when handling a request', () => {
let router: CurationRouter

Expand Down Expand Up @@ -470,54 +472,144 @@ describe('when handling a request', () => {

describe('when everything is fine', () => {
let req: AuthRequest

beforeEach(() => {
req = {
auth: { ethAddress: 'ethAddress' },
params: { id: 'some id' },
body: {
curation: {
status: CurationStatus.REJECTED,
},
},
} as any
})

describe('when updating a collection curation', () => {
let updateSpy: jest.SpyInstance<Promise<ItemAttributes>>
let expectedCuration: CollectionCurationAttributes

beforeEach(() => {
service = mockServiceWithAccess(CollectionCuration, true)
expectedCuration = {
id: 'uuid-123123-123123',
} as CollectionCurationAttributes
describe('and it only updates the status', () => {
beforeEach(() => {
req = {
auth: { ethAddress: 'ethAddress' },
params: { id: 'some id' },
body: {
curation: {
status: CurationStatus.REJECTED,
},
},
} as any
service = mockServiceWithAccess(CollectionCuration, true)
expectedCuration = {
id: 'uuid-123123-123123',
} as CollectionCurationAttributes

jest
.spyOn(service, 'getLatestById')
.mockResolvedValueOnce({ id: 'curationId' } as any)

jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)

updateSpy = jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)
})

jest
.spyOn(service, 'getLatestById')
.mockResolvedValueOnce({ id: 'curationId' } as any)
it('should resolve with the updated curation', async () => {
await expect(
router.updateCollectionCuration(req)
).resolves.toStrictEqual(expectedCuration)
})

jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)
it('should call the update method with the right data', async () => {
await router.updateCollectionCuration(req)

updateSpy = jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)
expect(updateSpy).toHaveBeenCalledWith('curationId', {
status: CurationStatus.REJECTED,
updated_at: expect.any(Date),
})
})
})

it('should resolve with the updated curation', async () => {
await expect(
router.updateCollectionCuration(req)
).resolves.toStrictEqual(expectedCuration)
describe('and it only updates the assignee', () => {
let assignee: string
beforeEach(() => {
assignee = mockAddress
req = {
auth: { ethAddress: 'ethAddress' },
params: { id: 'some id' },
body: {
curation: {
assignee,
},
},
} as any
service = mockServiceWithAccess(CollectionCuration, true)
expectedCuration = {
id: 'uuid-123123-123123',
} as CollectionCurationAttributes

jest
.spyOn(service, 'getLatestById')
.mockResolvedValueOnce({ id: 'curationId' } as any)

jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)

updateSpy = jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)
})

it('should resolve with the updated curation', async () => {
await expect(
router.updateCollectionCuration(req)
).resolves.toStrictEqual(expectedCuration)
})

it('should call the update method with the right data', async () => {
await router.updateCollectionCuration(req)

expect(updateSpy).toHaveBeenCalledWith('curationId', {
assignee: assignee.toLowerCase(),
updated_at: expect.any(Date),
})
})
})
describe('and it updates both the status & assignee', () => {
let assignee: string
beforeEach(() => {
assignee = mockAddress
req = {
auth: { ethAddress: 'ethAddress' },
params: { id: 'some id' },
body: {
curation: { status: CurationStatus.REJECTED, assignee },
},
} as any
service = mockServiceWithAccess(CollectionCuration, true)
expectedCuration = {
id: 'uuid-123123-123123',
} as CollectionCurationAttributes

jest
.spyOn(service, 'getLatestById')
.mockResolvedValueOnce({ id: 'curationId' } as any)

jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)

updateSpy = jest
.spyOn(service, 'updateById')
.mockResolvedValueOnce(expectedCuration)
})

it('should call the update method with the right data', async () => {
await router.updateCollectionCuration(req)
it('should resolve with the updated curation', async () => {
await expect(
router.updateCollectionCuration(req)
).resolves.toStrictEqual(expectedCuration)
})

expect(updateSpy).toHaveBeenCalledWith('curationId', {
status: CurationStatus.REJECTED,
updated_at: expect.any(Date),
it('should call the update method with the right data', async () => {
await router.updateCollectionCuration(req)

expect(updateSpy).toHaveBeenCalledWith('curationId', {
status: CurationStatus.REJECTED,
assignee: assignee.toLowerCase(),
updated_at: expect.any(Date),
})
})
})
})
Expand All @@ -528,6 +620,15 @@ describe('when handling a request', () => {
let expectedCuration: ItemCurationAttributes

beforeEach(() => {
req = {
auth: { ethAddress: 'ethAddress' },
params: { id: 'some id' },
body: {
curation: {
status: CurationStatus.REJECTED,
},
},
} as any
service = mockServiceWithAccess(ItemCuration, true)
expectedCuration = {
id: 'uuid-123123-123123',
Expand Down Expand Up @@ -739,9 +840,11 @@ describe('when handling a request', () => {
})

describe('when everything is fine', () => {
let assignee: string
let req: AuthRequest

beforeEach(() => {
assignee = mockAddress
jest
.spyOn(Collection, 'findOne')
.mockResolvedValueOnce({ ...dbCollectionMock })
Expand All @@ -752,6 +855,11 @@ describe('when handling a request', () => {
req = {
auth: { ethAddress: 'ethAddress' },
params: { id: 'some id' },
body: {
curation: {
assignee,
},
},
} as any
})

Expand Down Expand Up @@ -782,6 +890,7 @@ describe('when handling a request', () => {
status: CurationStatus.PENDING,
created_at: expect.any(Date),
updated_at: expect.any(Date),
assignee: assignee.toLowerCase(),
})
})
})
Expand Down
18 changes: 15 additions & 3 deletions src/Curation/Curation.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,17 @@ export class CurationRouter extends Router {
insertCollectionCuration = async (req: AuthRequest) => {
try {
const collectionId = server.extractFromReq(req, 'id')
let curationJSON: Partial<CollectionCurationAttributes> | undefined
try {
curationJSON = server.extractFromReq(req, 'curation')
} catch (error) {}
const ethAddress = req.auth.ethAddress

return this.insertCuration(
collectionId,
ethAddress,
CurationType.COLLECTION
CurationType.COLLECTION,
curationJSON
)
} catch (error) {
if (error instanceof NonExistentCollectionError) {
Expand Down Expand Up @@ -359,7 +364,10 @@ export class CurationRouter extends Router {
let fieldsToUpdate: Partial<
CollectionCurationAttributes & ItemCurationAttributes
> = {
status: curationJSON.status,
...(curationJSON.assignee
? { assignee: curationJSON.assignee.toLowerCase() }
: {}),
...(curationJSON.status ? { status: curationJSON.status } : {}),
updated_at: new Date(),
}

Expand All @@ -376,7 +384,8 @@ export class CurationRouter extends Router {
private insertCuration = async (
id: string,
ethAddress: string,
type: CurationType
type: CurationType,
curationJSON?: Partial<CollectionCurationAttributes>
) => {
const curationService = CurationService.byType(type)
await this.validateAccessToCuration(curationService, ethAddress, id)
Expand Down Expand Up @@ -409,6 +418,9 @@ export class CurationRouter extends Router {

if (type === CurationType.COLLECTION) {
attributes.collection_id = id
if (curationJSON?.assignee) {
attributes.assignee = curationJSON.assignee.toLowerCase()
}
}
if (type === CurationType.ITEM) {
attributes.item_id = id
Expand Down
3 changes: 2 additions & 1 deletion src/Curation/Curation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const patchCurationSchema = Object.freeze({
CurationStatus.REJECTED,
],
},
assignee: { type: ['string', 'null'] },
},
additionalProperties: false,
required: ['status'],
anyOf: [{ required: ['assignee'] }, { required: ['status'] }],
})

0 comments on commit d56747e

Please sign in to comment.