Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two routeOptions properties modelCustomIdType and modelCustomChildI… #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 101 additions & 16 deletions utilities/rest-helper-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ module.exports = function (logger, mongoose, server) {
resourceAliasForRoute = model.modelName;
}

var modelCustomIdType;
if (model.routeOptions) {
modelCustomIdType = model.routeOptions.customIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
}

var handler = HandlerHelper.generateFindHandler(model, options, Log);

var queryModel = joiMongooseHelper.generateJoiFindQueryModel(model, Log);
Expand Down Expand Up @@ -292,7 +300,7 @@ module.exports = function (logger, mongoose, server) {
validate: {
query: queryModel,
params: {
_id: Joi.objectId().required()
_id: modelCustomIdType.required()
},
headers: headersValidation
},
Expand Down Expand Up @@ -551,6 +559,14 @@ module.exports = function (logger, mongoose, server) {
policies.push(restHapiPolicies.logDelete(mongoose, model, Log));
}

var modelCustomIdType;
if (model.routeOptions) {
modelCustomIdType = model.routeOptions.customIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
}

server.route({
method: 'DELETE',
path: '/' + resourceAliasForRoute + "/{_id}",
Expand All @@ -562,7 +578,7 @@ module.exports = function (logger, mongoose, server) {
tags: ['api', collectionName],
validate: {
params: {
_id: Joi.objectId().required()
_id: modelCustomIdType.required()
},
payload: payloadModel,
headers: headersValidation
Expand Down Expand Up @@ -623,12 +639,20 @@ module.exports = function (logger, mongoose, server) {

var handler = HandlerHelper.generateDeleteHandler(model, options, Log);

var modelCustomIdType;
if (model.routeOptions) {
modelCustomIdType = model.routeOptions.customIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
}

var payloadModel = null;
if (config.enableSoftDelete) {
payloadModel = Joi.alternatives().try(Joi.array().items(Joi.object({ _id: Joi.objectId(), hardDelete: Joi.bool().default(false) })), Joi.array().items(Joi.objectId()));
payloadModel = Joi.alternatives().try(Joi.array().items(Joi.object({ _id: modelCustomIdType, hardDelete: Joi.bool().default(false) })), Joi.array().items(modelCustomIdType));
}
else {
payloadModel = Joi.array().items(Joi.objectId());
payloadModel = Joi.array().items(modelCustomIdType);
}

if (!config.enablePayloadValidation) {
Expand Down Expand Up @@ -742,6 +766,14 @@ module.exports = function (logger, mongoose, server) {
resourceAliasForRoute = model.modelName;
}

var modelCustomIdType;
if (model.routeOptions) {
modelCustomIdType = model.routeOptions.customIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
}

var handler = HandlerHelper.generateUpdateHandler(model, options, Log);

var updateModel = joiMongooseHelper.generateJoiUpdateModel(model, Log);
Expand Down Expand Up @@ -816,7 +848,7 @@ module.exports = function (logger, mongoose, server) {
tags: ['api', collectionName],
validate: {
params: {
_id: Joi.objectId().required()
_id: modelCustomIdType.required()
},
payload: updateModel,
headers: headersValidation
Expand Down Expand Up @@ -931,6 +963,17 @@ module.exports = function (logger, mongoose, server) {
policies.push(restHapiPolicies.logAdd(mongoose, ownerModel, childModel, association.type, Log));
}

var modelCustomIdType;
var modelCustomChildIdType;
if (ownerModel.routeOptions) {
modelCustomIdType = ownerModel.routeOptions.customIdType || Joi.objectId();
modelCustomChildIdType = ownerModel.routeOptions.customChildIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
modelCustomChildIdType = Joi.objectId();
}

server.route({
method: 'PUT',
path: '/' + ownerAlias + '/{ownerId}/' + childAlias + "/{childId}",
Expand All @@ -942,8 +985,8 @@ module.exports = function (logger, mongoose, server) {
tags: ['api', associationName, ownerModelName],
validate: {
params: {
ownerId: Joi.objectId().required(),
childId: Joi.objectId().required()
ownerId: modelCustomIdType.required(),
childId: modelCustomChildIdType.required()
},
payload: payloadValidation,
headers: headersValidation
Expand Down Expand Up @@ -1044,6 +1087,17 @@ module.exports = function (logger, mongoose, server) {
policies.push(restHapiPolicies.logRemove(mongoose, ownerModel, childModel, association.type, Log));
}

var modelCustomIdType;
var modelCustomChildIdType;
if (ownerModel.routeOptions) {
modelCustomIdType = ownerModel.routeOptions.customIdType || Joi.objectId();
modelCustomChildIdType = ownerModel.routeOptions.customChildIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
modelCustomChildIdType = Joi.objectId();
}

server.route({
method: 'DELETE',
path: '/' + ownerAlias + '/{ownerId}/' + childAlias + "/{childId}",
Expand All @@ -1055,8 +1109,8 @@ module.exports = function (logger, mongoose, server) {
tags: ['api', associationName, ownerModelName],
validate: {
params: {
ownerId: Joi.objectId().required(),
childId: Joi.objectId().required()
ownerId: modelCustomIdType.required(),
childId: modelCustomChildIdType.required()
},
headers: headersValidation
},
Expand Down Expand Up @@ -1117,22 +1171,34 @@ module.exports = function (logger, mongoose, server) {
var payloadValidation;
var label = "";

var modelCustomIdType;
var modelCustomChildIdType;
if (ownerModel.routeOptions) {
modelCustomIdType = ownerModel.routeOptions.customIdType || Joi.objectId();
modelCustomChildIdType = ownerModel.routeOptions.customChildIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
modelCustomChildIdType = Joi.objectId();
}


if (association.include && association.include.through) {
payloadValidation = joiMongooseHelper.generateJoiCreateModel(association.include.through, Log);
payloadValidation._inner.children = payloadValidation._inner.children.filter(function(key) {
return key.key !== ownerModelName && key.key !== childModelName;
});
label = payloadValidation._flags.label + "_many";
payloadValidation = payloadValidation.keys({
childId: Joi.objectId().description("the " + childModelName + "'s _id")
childId: modelCustomChildIdType.description("the " + childModelName + "'s _id")
});
payloadValidation = Joi.array().items(payloadValidation);

payloadValidation = Joi.alternatives().try(payloadValidation,
Joi.array().items(Joi.objectId())).label(label || "blank").required();
Joi.array().items(modelCustomChildIdType)).label(label || "blank").required();
}
else {
payloadValidation = Joi.array().items(Joi.objectId()).required();
payloadValidation = Joi.array().items(modelCustomChildIdType).required();
}

if (!config.enablePayloadValidation) {
Expand Down Expand Up @@ -1189,7 +1255,7 @@ module.exports = function (logger, mongoose, server) {
tags: ['api', associationName, ownerModelName],
validate: {
params: {
ownerId: Joi.objectId().required()
ownerId: modelCustomIdType.required()
},
payload: payloadValidation,
headers: headersValidation
Expand Down Expand Up @@ -1243,12 +1309,23 @@ module.exports = function (logger, mongoose, server) {

options = options || {};

var modelCustomIdType;
var modelCustomChildIdType;
if (ownerModel.routeOptions) {
modelCustomIdType = ownerModel.routeOptions.customIdType || Joi.objectId();
modelCustomChildIdType = ownerModel.routeOptions.customChildIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
modelCustomChildIdType = Joi.objectId();
}

var ownerAlias = ownerModel.routeOptions.alias || ownerModel.modelName;
var childAlias = association.alias || association.include.model.modelName;

var handler = HandlerHelper.generateAssociationRemoveManyHandler(ownerModel, association, options, Log);

var payloadValidation = Joi.array().items(Joi.objectId()).required();
var payloadValidation = Joi.array().items(modelCustomChildIdType).required();

payloadValidation = config.enablePayloadValidation ? payloadValidation : Joi.any();
payloadValidation = payloadValidation.description("An array of _ids to remove.")
Expand Down Expand Up @@ -1302,7 +1379,7 @@ module.exports = function (logger, mongoose, server) {
tags: ['api', associationName, ownerModelName],
validate: {
params: {
ownerId: Joi.objectId().required()
ownerId: modelCustomIdType.required()
},
payload: payloadValidation,
headers: headersValidation
Expand Down Expand Up @@ -1356,6 +1433,14 @@ module.exports = function (logger, mongoose, server) {
var ownerAlias = ownerModel.routeOptions.alias || ownerModel.modelName;
var childAlias = association.alias || association.include.model.modelName;

var modelCustomIdType;
if (ownerModel.routeOptions) {
modelCustomIdType = ownerModel.routeOptions.customIdType || Joi.objectId();
}
else {
modelCustomIdType = Joi.objectId();
}

var childModel = association.include.model;


Expand Down Expand Up @@ -1424,7 +1509,7 @@ module.exports = function (logger, mongoose, server) {
validate: {
query: queryModel,
params: {
ownerId: Joi.objectId().required()
ownerId: modelCustomIdType.required()
},
headers: headersValidation
},
Expand Down