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.
Merge pull request #320 from Sanketika-Obsrv/dataset-aliasing
fix: #OBS-I452 Dataset alias attach and detach functionality
- Loading branch information
Showing
8 changed files
with
168 additions
and
8 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,73 @@ | ||
import { Request, Response } from "express"; | ||
import { schemaValidation } from "../../services/ValidationService"; | ||
import DatasetAliasSchema from "./DatasetAliasValidationSchema.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, DatasetAliasSchema) | ||
if (!isRequestValid.isValid) { | ||
throw obsrvError("", "DATASET_ALIAS_INPUT_INVALID", isRequestValid.message, "BAD_REQUEST", 400) | ||
} | ||
|
||
const { dataset_id, action, alias_name: alias } = _.get(req, ["body", "request"]) | ||
let datasetAlias = alias | ||
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 (action === "attach") { | ||
if (_.get(dataset, "alias")) { | ||
throw obsrvError(dataset_id, "DATASET_ALIAS_EXISTS", `Dataset already has alias '${_.get(dataset, "alias")}' associated with it. Please detach the existing alias and try again`, "BAD_REQUEST", 400); | ||
} | ||
|
||
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.`, "BAD_REQUEST", 400); | ||
} | ||
} | ||
|
||
if (action === "detach") { | ||
const existingAliasName = _.get(dataset, "alias") | ||
if (!existingAliasName) { | ||
throw obsrvError(dataset_id, "DATASET_ALIAS_NOT_EXISTS", `Dataset '${dataset_id}' does not have any alias associated with it`, "BAD_REQUEST", 400); | ||
} | ||
datasetAlias = existingAliasName; | ||
} | ||
|
||
return datasetAlias | ||
} | ||
|
||
const datasetAlias = async (req: Request, res: Response) => { | ||
const dataset_alias = await validateRequest(req) | ||
const { dataset_id, action, alias_name } = _.get(req, ["body", "request"]) | ||
const userID = (req as any)?.userID; | ||
switch (action) { | ||
case "attach": | ||
await attachAlias(dataset_id, alias_name, userID); | ||
break; | ||
case "detach": | ||
await detachAlias(dataset_id, userID); | ||
break; | ||
} | ||
ResponseHandler.successResponse(req, res, { status: httpStatus.OK, data: { message: `Dataset alias name '${dataset_alias}' ${action}ed successfully`, dataset_id } }); | ||
} | ||
|
||
const attachAlias = async (dataset_id: string, alias_name: string, userID: string) => { | ||
await Dataset.update({ alias: alias_name, updated_by: userID }, { where: { id: dataset_id } }); | ||
} | ||
|
||
const detachAlias = async (dataset_id: string, userID: string) => { | ||
await Dataset.update({ alias: null, updated_by: userID }, { where: { id: dataset_id } }); | ||
} | ||
|
||
export default datasetAlias; |
77 changes: 77 additions & 0 deletions
77
api-service/src/controllers/DatasetAlias/DatasetAliasValidationSchema.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,77 @@ | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"id": { | ||
"type": "string", | ||
"enum": [ | ||
"api.datasets.alias" | ||
] | ||
}, | ||
"ver": { | ||
"type": "string" | ||
}, | ||
"ts": { | ||
"type": "string" | ||
}, | ||
"params": { | ||
"type": "object", | ||
"properties": { | ||
"msgid": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"msgid" | ||
], | ||
"additionalProperties": false | ||
}, | ||
"request": { | ||
"type": "object", | ||
"properties": { | ||
"action": { | ||
"type": "string", | ||
"enum": [ | ||
"attach", | ||
"detach" | ||
] | ||
}, | ||
"dataset_id": { | ||
"type": "string" | ||
}, | ||
"alias_name": { | ||
"type": "string" | ||
} | ||
}, | ||
"if": { | ||
"properties": { | ||
"action": { | ||
"const": "attach" | ||
} | ||
} | ||
}, | ||
"then": { | ||
"properties": { | ||
"alias_name": { | ||
"minLength": 1 | ||
} | ||
}, | ||
"required": [ | ||
"alias_name" | ||
] | ||
}, | ||
"required": [ | ||
"action", | ||
"dataset_id" | ||
], | ||
"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
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
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
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