Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehewitt15 committed Aug 2, 2024
1 parent b55f596 commit af4d19a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 57 deletions.
126 changes: 70 additions & 56 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,49 +347,59 @@ export class DdoDatabase {
}

getDDOSchema(ddo: Record<string, any>): Schema {
// Find the schema based on the DDO version OR use the short DDO schema when state !== 0
let schemaName: string
if (ddo.nft?.state !== 0) {
schemaName = 'op_ddo_short'
} else if (ddo.version) {
schemaName = `op_ddo_v${ddo.version}`
try {
// Find the schema based on the DDO version OR use the short DDO schema when state !== 0
let schemaName: string
if (ddo.nft?.state !== 0) {
schemaName = 'op_ddo_short'
} else if (ddo.version) {
schemaName = `op_ddo_v${ddo.version}`
}
const schema = this.schemas.find((s) => s.name === schemaName)
DATABASE_LOGGER.logMessageWithEmoji(
`Returning schema: ${schemaName}`,
true,
GENERIC_EMOJIS.EMOJI_OCEAN_WAVE,
LOG_LEVELS_STR.LEVEL_INFO
)
return schema
} catch (error) {
DATABASE_LOGGER.error(`Error when getting schema for DDO with id: ${ddo.id}`)
return null
}
const schema = this.schemas.find((s) => s.name === schemaName)
DATABASE_LOGGER.logMessageWithEmoji(
`Returning schema: ${schemaName}`,
true,
GENERIC_EMOJIS.EMOJI_OCEAN_WAVE,
LOG_LEVELS_STR.LEVEL_INFO
)
return schema
}

async validateDDO(ddo: Record<string, any>): Promise<boolean> {
if (ddo.nft?.state !== 0) {
// Skipping validation for short DDOs as it currently doesn't work
// TODO: DDO validation needs to be updated to consider the fields required by the schema
// See github issue: https://github.com/oceanprotocol/ocean-node/issues/256
return true
} else {
const validation = await validateObject(ddo, ddo.chainId, ddo.nftAddress)
if (validation[0] === true) {
DATABASE_LOGGER.logMessageWithEmoji(
`Validation of DDO with did: ${ddo.id} has passed`,
true,
GENERIC_EMOJIS.EMOJI_OCEAN_WAVE,
LOG_LEVELS_STR.LEVEL_INFO
)
try {
if (ddo.nft?.state !== 0) {
// Skipping validation for short DDOs as it currently doesn't work
// TODO: DDO validation needs to be updated to consider the fields required by the schema
// See github issue: https://github.com/oceanprotocol/ocean-node/issues/256
return true
} else {
DATABASE_LOGGER.logMessageWithEmoji(
`Validation of DDO with schema version ${ddo.version} failed with errors: ` +
JSON.stringify(validation[1]),
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_ERROR
)
return false
const validation = await validateObject(ddo, ddo.chainId, ddo.nftAddress)
if (validation[0] === true) {
DATABASE_LOGGER.logMessageWithEmoji(
`Validation of DDO with did: ${ddo.id} has passed`,
true,
GENERIC_EMOJIS.EMOJI_OCEAN_WAVE,
LOG_LEVELS_STR.LEVEL_INFO
)
return true
} else {
DATABASE_LOGGER.logMessageWithEmoji(
`Validation of DDO with schema version ${ddo.version} failed with errors: ` +
JSON.stringify(validation[1]),
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_ERROR
)
return false
}
}
} catch (error) {
DATABASE_LOGGER.error(`Error when validating DDO with id: ${ddo.id}`)
return false
}
}

Expand Down Expand Up @@ -442,29 +452,33 @@ export class DdoDatabase {
}

async create(ddo: Record<string, any>) {
const schema = this.getDDOSchema(ddo)
if (!schema) {
throw new Error(`Schema for version ${ddo.version} not found`)
}
try {
const validation = await this.validateDDO(ddo)
if (validation === true) {
return await this.provider
.collections(schema.name)
.documents()
.create({ ...ddo })
} else {
throw new Error(`Validation of DDO with schema version ${ddo.version} failed`)
const schema = this.getDDOSchema(ddo)
if (!schema) {
throw new Error(`Schema for version ${ddo.version} not found`)
}
try {
const validation = await this.validateDDO(ddo)
if (validation === true) {
return await this.provider
.collections(schema.name)
.documents()
.create({ ...ddo })
} else {
throw new Error(`Validation of DDO with schema version ${ddo.version} failed`)
}
} catch (error) {
const errorMsg = `Error when creating DDO entry ${ddo.id}: ` + error.message
DATABASE_LOGGER.logMessageWithEmoji(
errorMsg,
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_ERROR
)
return null
}
} catch (error) {
const errorMsg = `Error when creating DDO entry ${ddo.id}: ` + error.message
DATABASE_LOGGER.logMessageWithEmoji(
errorMsg,
true,
GENERIC_EMOJIS.EMOJI_CROSS_MARK,
LOG_LEVELS_STR.LEVEL_ERROR
)
return null
DATABASE_LOGGER.error(`Error when creating DDO entry ${ddo.id}: ${error.message}`)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/integration/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ describe('LogDatabase retrieveMultipleLogs with pagination', () => {
})

it('should return empty results for a non-existent page', async () => {
const nonExistentPage = 300 // Assuming this page doesn't exist
const nonExistentPage = 30000 // Assuming this page doesn't exist
const logs = await database.logs.retrieveMultipleLogs(
new Date(Date.now() - 10000), // 10 seconds ago
new Date(), // now
Expand Down

0 comments on commit af4d19a

Please sign in to comment.