forked from Sunbird-Obsrv/obsrv-api-service
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: #OBS-I452 Dataset alias attach and detach functionality
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Request, Response } from "express"; | ||
import { schemaValidation } from "../../services/ValidationService"; | ||
import AttachAliasSchema from "./AttachAliasValidationSchema.json" | ||
import { obsrvError } from "../../types/ObsrvError"; | ||
import _ from "lodash"; | ||
import { datasetService } from "../../services/DatasetService"; | ||
import { Op } from "sequelize"; | ||
import { Dataset } from "../../models/Dataset"; | ||
import { ResponseHandler } from "../../helpers/ResponseHandler"; | ||
import httpStatus from "http-status"; | ||
|
||
|
||
const validateRequest = async (req: Request) => { | ||
|
||
const isRequestValid: Record<string, any> = schemaValidation(req.body, AttachAliasSchema) | ||
if (!isRequestValid.isValid) { | ||
throw obsrvError("", "DATASET_ALIAS_INPUT_INVALID", isRequestValid.message, "BAD_REQUEST", 400) | ||
} | ||
|
||
const { dataset_id, alias_name: alias } = _.get(req, ["body", "request"]) | ||
const dataset = await datasetService.getDataset(dataset_id, ["id", "name", "alias"], true); | ||
if (_.isEmpty(dataset)) { | ||
throw obsrvError(dataset_id, "DATASET_NOT_EXISTS", `Dataset does not exists with id:${dataset_id}`, "NOT_FOUND", 404); | ||
} | ||
|
||
if (_.get(dataset, "alias")) { | ||
throw obsrvError(dataset_id, "DATASET_ALIAS_EXISTS", `Dataset already has alias associated with it. Please detach the existing alias and try again`, "CONFLICT", 409); | ||
} | ||
|
||
const datasetList = await datasetService.findDatasets({ [Op.or]: [{ dataset_id: alias }, { name: alias }, { alias }] }, ["id"]); | ||
const draftDatasetList = await datasetService.findDraftDatasets({ [Op.or]: [{ dataset_id: alias }, { name: alias }] }, ["id"]); | ||
if (!(_.isEmpty(datasetList) && _.isEmpty(draftDatasetList))) { | ||
throw obsrvError(dataset_id, "DATASET_ALIAS_NOT_UNIQUE", `Dataset alias must be unique. The alias '${alias}' cannot be the same as the dataset id, dataset name or alias name of any other dataset.`, "CONFLICT", 409); | ||
} | ||
|
||
} | ||
|
||
const attachAlias = async (req: Request, res: Response) => { | ||
await validateRequest(req) | ||
const { dataset_id, alias_name } = _.get(req, ["body", "request"]) | ||
await Dataset.update({ alias: alias_name }, { where: { id: dataset_id } }); | ||
ResponseHandler.successResponse(req, res, { status: httpStatus.OK, data: { message: `Dataset alias name attached successfully`, dataset_id } }); | ||
} | ||
|
||
export default attachAlias; |
54 changes: 54 additions & 0 deletions
54
api-service/src/controllers/DatasetAlias/AttachAliasValidationSchema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "string", | ||
"enum": [ | ||
"api.datasets.alias.attach" | ||
] | ||
}, | ||
"ver": { | ||
"type": "string" | ||
}, | ||
"ts": { | ||
"type": "string" | ||
}, | ||
"params": { | ||
"type": "object", | ||
"properties": { | ||
"msgid": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"msgid" | ||
], | ||
"additionalProperties": false | ||
}, | ||
"request": { | ||
"type": "object", | ||
"properties": { | ||
"dataset_id": { | ||
"type": "string" | ||
}, | ||
"alias_name": { | ||
"type": "string", | ||
"minLength": 1 | ||
} | ||
}, | ||
"required": [ | ||
"dataset_id", | ||
"alias_name" | ||
], | ||
"additionalProperties": false | ||
} | ||
}, | ||
"required": [ | ||
"id", | ||
"ver", | ||
"ts", | ||
"params", | ||
"request" | ||
], | ||
"additionalProperties": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Request, Response } from "express"; | ||
import { datasetService } from "../../services/DatasetService"; | ||
import { obsrvError } from "../../types/ObsrvError"; | ||
import { Dataset } from "../../models/Dataset"; | ||
import { ResponseHandler } from "../../helpers/ResponseHandler"; | ||
import httpStatus from "http-status"; | ||
import _ from "lodash"; | ||
|
||
const validateDataset = async (dataset_id: string) => { | ||
const dataset = await datasetService.getDataset(dataset_id, ["alias"], true); | ||
if (!dataset) { | ||
throw obsrvError(dataset_id, "DATASET_NOT_FOUND", `Dataset with the given dataset_id:${dataset_id} not found`, "NOT_FOUND", 404); | ||
} | ||
if (!_.get(dataset, "alias")) { | ||
throw obsrvError(dataset_id, "DATASET_ALIAS_NOT_EXISTS", `Dataset does not have any alias associated with it`, "NOT_FOUND", 404); | ||
} | ||
return _.get(dataset, "alias") | ||
} | ||
|
||
const detachAlias = async (req: Request, res: Response) => { | ||
const dataset_id = req.params.dataset_id | ||
const alias_name=await validateDataset(dataset_id); | ||
await Dataset.update({ alias: null }, { where: { id: dataset_id } }); | ||
ResponseHandler.successResponse(req, res, { status: httpStatus.OK, data: { message: `Dataset alias name '${alias_name}' detached successfully`, dataset_id } }); | ||
} | ||
|
||
export default detachAlias; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters