Skip to content

Commit

Permalink
Merge pull request #122 from pshenmic/feat/data_contract_documents_count
Browse files Browse the repository at this point in the history
Add documents count in the data contract lists
  • Loading branch information
pshenmic authored May 1, 2024
2 parents 333ce45 + 32e3fbb commit 3482771
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
7 changes: 5 additions & 2 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ GET /dataContract/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec
version: 0,
txHash: "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
timestamp: "2024-03-18T10:13:54.150Z",
isSystem: false
isSystem: false,
documentsCount: 1337
}
```
Response codes:
Expand Down Expand Up @@ -223,7 +224,8 @@ GET /dataContracts?page=1&limit=10&order=asc&order_by=block_height
version: 0,
txHash: "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
timestamp: "2024-03-18T10:13:54.150Z",
isSystem: false
isSystem: false,
documentsCount: 1337
}, ...
]
}
Expand Down Expand Up @@ -372,6 +374,7 @@ GET /identities/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec/dataContracts?page=
txHash: "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
timestamp: "2024-03-18T10:13:54.150Z",
isSystem: false
documentsCount: 1337
}, ...
]
}
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/dao/DataContractsDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = class DataContractsDAO {
.select('data_contracts.identifier as identifier', 'data_contracts.owner as owner',
'data_contracts.schema as schema', 'data_contracts.is_system as is_system',
'data_contracts.version as version', 'state_transitions.hash as tx_hash', 'blocks.timestamp as timestamp')
.select(this.knex('documents').count('*').whereRaw('documents.data_contract_id = id').as('documents_count'))
.leftJoin('state_transitions', 'data_contracts.state_transition_hash', 'state_transitions.hash')
.leftJoin('blocks', 'blocks.hash', 'state_transitions.block_hash')
.where('data_contracts.identifier', identifier)
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/dao/IdentitiesDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ module.exports = class IdentitiesDAO {
.as('data_contracts')

const rows = await this.knex(filteredDataContracts)
.select('identifier', 'data_contract_owner', 'version', 'tx_hash', 'rank', 'total_count', 'row_number', 'is_system', 'blocks.timestamp as timestamp')
.select('data_contracts.id as id', 'identifier', 'data_contract_owner', 'version', 'tx_hash', 'rank', 'total_count', 'row_number', 'is_system', 'blocks.timestamp as timestamp')
.select(this.knex('documents').count('*').whereRaw('documents.data_contract_id = id').as('documents_count'))
.leftJoin('state_transitions', 'state_transitions.hash', 'tx_hash')
.leftJoin('blocks', 'blocks.hash', 'state_transitions.block_hash')
.whereBetween('row_number', [fromRank, toRank])
Expand Down
8 changes: 5 additions & 3 deletions packages/api/src/models/DataContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ module.exports = class DataContract {
txHash
timestamp
isSystem
documentsCount

constructor (identifier, owner, schema, version, txHash, timestamp, isSystem) {
constructor (identifier, owner, schema, version, txHash, timestamp, isSystem, documentsCount) {
this.identifier = identifier ? identifier.trim() : null
this.owner = owner ? owner.trim() : null
this.schema = schema ?? null
this.version = version ?? null
this.txHash = txHash ?? null
this.timestamp = timestamp ?? null
this.isSystem = isSystem ?? null
this.documentsCount = documentsCount ?? null
}

// eslint-disable-next-line camelcase
static fromRow ({ identifier, owner, schema, version, tx_hash, timestamp, is_system }) {
return new DataContract(identifier, owner, schema ? JSON.stringify(schema) : null, version, tx_hash, timestamp, is_system)
static fromRow ({ identifier, owner, schema, version, tx_hash, timestamp, is_system, documents_count }) {
return new DataContract(identifier, owner, schema ? JSON.stringify(schema) : null, version, tx_hash, timestamp, is_system, Number(documents_count))
}
}
18 changes: 12 additions & 6 deletions packages/api/test/integration/data.contracts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ describe('DataContracts routes', () => {
version: 0,
txHash: dataContract.is_system ? null : transaction.hash,
timestamp: dataContract.is_system ? null : block.timestamp.toISOString(),
isSystem: dataContract.is_system
isSystem: dataContract.is_system,
documentsCount: 0
}))

assert.equal(body.resultSet.length, 10)
Expand All @@ -127,7 +128,8 @@ describe('DataContracts routes', () => {
version: 0,
txHash: dataContract.is_system ? null : transaction.hash,
timestamp: dataContract.is_system ? null : block.timestamp.toISOString(),
isSystem: dataContract.is_system
isSystem: dataContract.is_system,
documentsCount: 0
}))

assert.equal(body.resultSet.length, 10)
Expand All @@ -153,7 +155,8 @@ describe('DataContracts routes', () => {
version: 0,
txHash: dataContract.is_system ? null : transaction.hash,
timestamp: dataContract.is_system ? null : block.timestamp.toISOString(),
isSystem: dataContract.is_system
isSystem: dataContract.is_system,
documentsCount: 0
}))

assert.equal(body.resultSet.length, 6)
Expand All @@ -179,7 +182,8 @@ describe('DataContracts routes', () => {
version: 0,
txHash: dataContract.is_system ? null : transaction.hash,
timestamp: dataContract.is_system ? null : block.timestamp.toISOString(),
isSystem: dataContract.is_system
isSystem: dataContract.is_system,
documentsCount: 0
}))

assert.equal(body.resultSet.length, 6)
Expand Down Expand Up @@ -207,7 +211,8 @@ describe('DataContracts routes', () => {
version: 0,
txHash: dataContract.is_system ? null : transaction.hash,
timestamp: dataContract.is_system ? null : block.timestamp.toISOString(),
isSystem: dataContract.is_system
isSystem: dataContract.is_system,
documentsCount: 0
}))

assert.equal(body.resultSet.length, 10)
Expand All @@ -233,7 +238,8 @@ describe('DataContracts routes', () => {
version: 0,
txHash: null,
timestamp: null,
isSystem: true
isSystem: true,
documentsCount: 0
}

assert.deepEqual(body, expectedDataContract)
Expand Down
12 changes: 8 additions & 4 deletions packages/api/test/integration/identities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ describe('Identities routes', () => {
schema: null,
txHash: _dataContract.transaction.hash,
timestamp: _dataContract.block.timestamp.toISOString(),
isSystem: false
isSystem: false,
documentsCount: 0
}))
assert.deepEqual(body.resultSet, expectedDataContracts)
})
Expand Down Expand Up @@ -401,7 +402,8 @@ describe('Identities routes', () => {
schema: null,
txHash: _dataContract.transaction.hash,
timestamp: _dataContract.block.timestamp.toISOString(),
isSystem: false
isSystem: false,
documentsCount: 0
}))
assert.deepEqual(body.resultSet, expectedDataContracts)
})
Expand Down Expand Up @@ -444,7 +446,8 @@ describe('Identities routes', () => {
schema: null,
txHash: _dataContract.transaction.hash,
timestamp: _dataContract.block.timestamp.toISOString(),
isSystem: false
isSystem: false,
documentsCount: 0
}))
assert.deepEqual(body.resultSet, expectedDataContracts)
})
Expand Down Expand Up @@ -487,7 +490,8 @@ describe('Identities routes', () => {
schema: null,
txHash: _dataContract.transaction.hash,
timestamp: _dataContract.block.timestamp.toISOString(),
isSystem: false
isSystem: false,
documentsCount: 0
}))
assert.deepEqual(body.resultSet, expectedDataContracts)
})
Expand Down
3 changes: 2 additions & 1 deletion packages/api/test/integration/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ describe('Other routes', () => {
version: 0,
txHash: dataContractTransaction.hash,
timestamp: block.timestamp.toISOString(),
isSystem: false
isSystem: false,
documentsCount: 0
}

assert.deepEqual({ dataContract: expectedDataContract }, body)
Expand Down
7 changes: 5 additions & 2 deletions packages/frontend/src/app/api/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ GET /dataContract/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec
version: 0,
txHash: "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
timestamp: "2024-03-18T10:13:54.150Z",
isSystem: false
isSystem: false,
documentsCount: 1337
}
```
Response codes:
Expand Down Expand Up @@ -192,7 +193,8 @@ GET /dataContracts?page=1&limit=10&order=asc&order_by=block_height
version: 0,
txHash: "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
timestamp: "2024-03-18T10:13:54.150Z",
isSystem: false
isSystem: false,
documentsCount: 1337
}, ...
]
}
Expand Down Expand Up @@ -341,6 +343,7 @@ GET /identities/GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec/dataContracts?page=
txHash: "DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF",
timestamp: "2024-03-18T10:13:54.150Z",
isSystem: false
documentsCount: 1337
}, ...
]
}
Expand Down

0 comments on commit 3482771

Please sign in to comment.