Skip to content

Commit

Permalink
feat: 🎸 Country Code Mapping Table Functionality
Browse files Browse the repository at this point in the history
✅ Closes: 18
  • Loading branch information
lovel8 committed Mar 27, 2024
1 parent bd5d882 commit 2442126
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
},
"dependencies": {
"fastify": "^4.26.2",
"i18n-iso-countries": "^7.11.0",
"ts-node": "^10.9.2"
}
}
16 changes: 10 additions & 6 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ function datasetCommand(yargs: yargs.Argv<{}>): {
},
}
)
.command("getAllCountriesCallingCode", "Get all countries calling code")
.command("isWinner", "The account is the dataset auditor winner", {
datasetId: {
description: "Dataset Id",
Expand Down Expand Up @@ -513,6 +514,15 @@ async function dataset(
path: String(argv.path),
})
break
case "getDatasetState":
await new DatasetMetadatas().getDatasetState({
context,
datasetId: Number(argv.datasetId),
})
break
case "getAllCountriesCallingCode":
await new DatasetMetadatas().getAllCountriesCallingCode()
break
case "submitDatasetProof":
await new DatasetProofs().submitDatasetProof({
context,
Expand Down Expand Up @@ -561,12 +571,6 @@ async function dataset(
path: String(argv.path),
})
break
case "getDatasetState":
await new DatasetMetadatas().getDatasetState({
context,
datasetId: Number(argv.datasetId),
})
break
default:
console.log("Unknown command.")
}
Expand Down
49 changes: 41 additions & 8 deletions src/dataset/metadata/repo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
********************************************************************************/

import fs from "fs"
import * as countries from "i18n-iso-countries"
import { DatasetState } from "@dataswapjs/dataswapjs"
import { handleEvmError, logMethodCall } from "../../../shared/utils/utils"
import { chainSuccessInterval, blockPeriod } from "../../../shared/constant"
Expand Down Expand Up @@ -192,14 +193,13 @@ export class DatasetMetadatas {
fs.readFileSync(options.path).toString()
) as DatasetReplicaRequirements

const state = await handleEvmError(
options.context.evm.datasetMetadata.getDatasetState(
datasetReplicaRequirements.datasetId
)
)
if (state != DatasetState.MetadataSubmitted) {
console.log("Dataset state is not MetadataSubmitted, do nothing~")
return true
if (
!(await this.checkSubmissionRequirementsCriteria({
context: options.context,
datasetReplicaRequirements,
}))
) {
return false
}

options.context.evm.datasetRequirement
Expand Down Expand Up @@ -236,4 +236,37 @@ export class DatasetMetadatas {
)
)
}

@logMethodCall(["context"])
async getAllCountriesCallingCode(): Promise<{
[numericKey: string]: string
}> {
return countries.getNumericCodes()
}

private async checkSubmissionRequirementsCriteria(options: {
context: Context
datasetReplicaRequirements: DatasetReplicaRequirements
}): Promise<boolean> {
const state = await handleEvmError(
options.context.evm.datasetMetadata.getDatasetState(
options.datasetReplicaRequirements.datasetId
)
)
if (state != DatasetState.MetadataSubmitted) {
console.log("Dataset state is not MetadataSubmitted, do nothing~")
return false
}
for (const country of options.datasetReplicaRequirements.countrys) {
if (!countries.isValid(Number(country))) {
console.log(
"Dataset replic requirements countrys invalid:",
country
)
return false
}
}

return true
}
}
12 changes: 6 additions & 6 deletions testdata/datasetReplicaRequirements.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
4211
],
"countrys": [
517,
1300,
2040,
3063,
4687
840,
528,
508,
504,
533
],
"citys": [
[
Expand Down Expand Up @@ -95,5 +95,5 @@
]
],
"amount": 0,
"datasetId": 16
"datasetId": 17
}

0 comments on commit 2442126

Please sign in to comment.