Skip to content

Commit

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

getDDOSchema(ddo: Record<string, any>): Schema {
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
// 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
}

async validateDDO(ddo: Record<string, any>): Promise<boolean> {
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
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
)
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
)
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
}
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 @@ -452,33 +442,29 @@ 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 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
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) {
DATABASE_LOGGER.error(`Error when creating DDO entry ${ddo.id}: ${error.message}`)
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
}
}

Expand Down

0 comments on commit d67199f

Please sign in to comment.