Skip to content

Commit

Permalink
Merge 8829f18 into ff6afcc
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Mar 9, 2022
2 parents ff6afcc + 8829f18 commit dc5cf61
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 33 deletions.
26 changes: 26 additions & 0 deletions migrations/1646234502768_add-cheque-signature-column.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'
import { SlotUsageCheque } from '../src/SlotUsageCheque'

const tableName = SlotUsageCheque.tableName
const oldColumnName = 'signedMessage'

const columns = {
signature: { type: 'TEXT', notNull: true },
salt: { type: 'VARCHAR(66)', notNull: true },
qty: { type: 'INT', notNull: true },
}

export const shorthands: ColumnDefinitions | undefined = undefined

export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.dropColumn(tableName, oldColumnName)
pgm.addColumn(tableName, columns)
}

export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.dropColumn(tableName, columns)
pgm.addColumn(tableName, {
[oldColumnName]: { type: 'TEXT', notNull: true },
})
}
95 changes: 72 additions & 23 deletions src/Collection/Collection.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1244,8 +1244,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds: [],
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(400)
.then((response: any) => {
Expand All @@ -1269,8 +1273,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds: [dbTPItemMock.id],
signedMessage: 'invalid',
signature: 'signature',
cheque: {
signedMessage: 'invalid',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(400)
.then((response: any) => {
Expand Down Expand Up @@ -1307,8 +1315,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(400)
.then((response: any) => {
Expand Down Expand Up @@ -1337,8 +1349,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds: [dbItemMock.id],
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(409)
.then((response: any) => {
Expand Down Expand Up @@ -1396,24 +1412,33 @@ describe('Collection router', () => {
describe('and the item collection does not have a virtual curation', () => {
it('should create a SlotUsageCheque record with the request data', () => {
const signedMessage = 'a signed message'
const signature = 'signature'
const qty = 1
const salt = '0xsalt'

return server
.post(buildURL(url))
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage,
signature: 'signature',
cheque: {
signedMessage,
signature,
qty,
salt,
},
})
.expect(200)
.then(() => {
expect(slotUsageChequeCreateSpy).toHaveBeenCalledWith({
id: expect.any(String),
signedMessage,
signature,
collection_id: dbTPCollection.id,
third_party_id: dbTPCollection.third_party_id,
created_at: expect.any(Date),
updated_at: expect.any(Date),
qty,
salt,
})
})
})
Expand All @@ -1424,8 +1449,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(200)
.then(() => {
Expand All @@ -1450,8 +1479,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(200)
.then(() => {
Expand Down Expand Up @@ -1483,8 +1516,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(200)
.then(() => {
Expand Down Expand Up @@ -1512,8 +1549,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(200)
.then((response: any) => {
Expand Down Expand Up @@ -1543,8 +1584,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(200)
.then(() => {
Expand All @@ -1563,8 +1608,12 @@ describe('Collection router', () => {
.set(createAuthHeaders('post', url))
.send({
itemIds,
signedMessage: 'message',
signature: 'signature',
cheque: {
signedMessage: 'message',
signature: 'signature',
qty: 1,
salt: '0xsalt',
},
})
.expect(200)
.then(() => {
Expand Down
21 changes: 15 additions & 6 deletions src/Collection/Collection.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { buildCollectionForumPost, createPost } from '../Forum'
import { sendDataToWarehouse } from '../warehouse'
import { Collection } from './Collection.model'
import { CollectionService } from './Collection.service'
import { CollectionAttributes, FullCollection } from './Collection.types'
import {
PublishCheque,
CollectionAttributes,
FullCollection,
} from './Collection.types'
import { upsertCollectionSchema, saveTOSSchema } from './Collection.schema'
import { hasPublicAccess } from './access'
import { hasTPCollectionURN, isTPCollection, toFullCollection } from './utils'
Expand Down Expand Up @@ -245,22 +249,27 @@ export class CollectionRouter extends Router {

publishCollection = async (
req: AuthRequest
): Promise<{ collection: FullCollection; items: FullItem[] }> => {
): Promise<{
collection: FullCollection
items: FullItem[]
}> => {
const id = server.extractFromReq(req, 'id')

try {
const dbCollection = await this.service.getDBCollection(id)

let result: { collection: CollectionAttributes; items: FullItem[] }
let result: {
collection: CollectionAttributes
items: FullItem[]
}

if (isTPCollection(dbCollection)) {
const itemIds = server.extractFromReq<string[]>(req, 'itemIds')
const dbItems = await Item.findByIds(itemIds)

result = await this.service.publishTPCollection(
dbCollection,
dbItems,
server.extractFromReq(req, 'signedMessage'),
server.extractFromReq(req, 'signature')
server.extractFromReq<PublishCheque>(req, 'cheque')
)

// Eventually, posting to the forum will be done from the server for both collection types (https://github.com/decentraland/builder/issues/1754)
Expand Down
10 changes: 7 additions & 3 deletions src/Collection/Collection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { decodeTPCollectionURN, isTPCollection, toDBCollection } from './utils'
import {
CollectionAttributes,
FullCollection,
PublishCheque,
ThirdPartyCollectionAttributes,
} from './Collection.types'
import { Collection } from './Collection.model'
Expand Down Expand Up @@ -140,8 +141,7 @@ export class CollectionService {
public async publishTPCollection(
dbCollection: ThirdPartyCollectionAttributes,
dbItems: ItemAttributes[],
signedMessage: string,
signature: string
cheque: PublishCheque
): Promise<{
collection: CollectionAttributes
items: FullItem[]
Expand All @@ -155,6 +155,8 @@ export class CollectionService {

// There'll always be a publish before a PUSH CHANGES, so this method also creates or updates the virtual CollectionCuration for the items

const { signedMessage, signature, qty, salt } = cheque

if (dbItems.length === 0) {
throw new InvalidRequestError('Tried to publish no TP items')
}
Expand Down Expand Up @@ -192,7 +194,9 @@ export class CollectionService {
const now = new Date()
await SlotUsageCheque.create<SlotUsageChequeAttributes>({
id: uuid(),
signedMessage,
signature,
qty,
salt,
collection_id: dbCollection.id,
third_party_id: dbCollection.third_party_id,
created_at: now,
Expand Down
7 changes: 7 additions & 0 deletions src/Collection/Collection.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ export type FullCollection = Omit<
> & {
urn: string | null
}

export type PublishCheque = {
signedMessage: string
signature: string
qty: number
salt: string
}
4 changes: 3 additions & 1 deletion src/SlotUsageCheque/SlotUsageCheque.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export type SlotUsageChequeAttributes = {
id: string
signedMessage: string
qty: number
salt: string
signature: string
collection_id: string
third_party_id: string
updated_at: Date
Expand Down

0 comments on commit dc5cf61

Please sign in to comment.