labs-angular-backend
Advanced tools
Comparing version 1.0.3 to 1.0.4
import { UsersService } from "../../services/UsersService"; | ||
import { IUser, PartialUser } from "../../models/User"; | ||
import { IUser } from "../../models/User"; | ||
export declare class UserCtrl { | ||
private usersService; | ||
constructor(usersService: UsersService); | ||
/** | ||
* Authenticate a user. | ||
* @param email | ||
* @param password | ||
* @returns {IUser} | ||
*/ | ||
authenticate(email: string, password: string): IUser; | ||
getByEmail(email: string): IUser; | ||
/** | ||
* Find a user by is mail or id. | ||
* @param idOrMail | ||
* @returns {IUser} | ||
*/ | ||
get(idOrMail: string): IUser; | ||
updateStatus(email: string, status: string): IUser; | ||
/** | ||
* | ||
* @param id | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
update(id: string, user: any): IUser; | ||
create(user: any): IUser; | ||
remove(): void; | ||
getList(): PartialUser[]; | ||
/** | ||
* | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
create(user: IUser): IUser; | ||
/** | ||
* Remove the user. | ||
* @param id | ||
* @returns {any} | ||
*/ | ||
remove(id: string): IUser; | ||
/** | ||
* Get All users. | ||
* @returns {IUser[]} | ||
*/ | ||
getList(): IUser[]; | ||
} |
@@ -23,2 +23,8 @@ "use strict"; | ||
} | ||
/** | ||
* Authenticate a user. | ||
* @param email | ||
* @param password | ||
* @returns {IUser} | ||
*/ | ||
UserCtrl.prototype.authenticate = function (email, password) { | ||
@@ -28,3 +34,3 @@ ts_log_debug_1.$log.debug("authenticate user with email", email, " & password ", password); | ||
ts_log_debug_1.$log.debug("find user by email", user); | ||
if (null == user) { | ||
if (!user) { | ||
throw new ts_httpexceptions_1.NotFound("authentication failed, user not found"); | ||
@@ -35,28 +41,84 @@ } | ||
} | ||
return this.usersService.update(user._id, { status: "online" }); | ||
}; | ||
/** | ||
* Find a user by is mail or id. | ||
* @param idOrMail | ||
* @returns {IUser} | ||
*/ | ||
UserCtrl.prototype.get = function (idOrMail) { | ||
var user = this.usersService.findByEmail(idOrMail) || this.usersService.find(idOrMail); | ||
if (!user) { | ||
throw new ts_httpexceptions_1.NotFound("User not found."); | ||
} | ||
return user; | ||
}; | ||
UserCtrl.prototype.getByEmail = function (email) { | ||
return this.usersService.findByEmail(email); | ||
}; | ||
UserCtrl.prototype.updateStatus = function (email, status) { | ||
if (!this.usersService.checkStatus(status)) { | ||
throw new ts_httpexceptions_1.BadRequest("Wrong status"); | ||
} | ||
if (!this.usersService.checkEmail(email)) { | ||
throw new ts_httpexceptions_1.BadRequest("Wrong email"); | ||
} | ||
var user = this.usersService.findByEmail(email); | ||
if (!user) { | ||
throw new ts_httpexceptions_1.NotFound("User not found."); | ||
} | ||
ts_log_debug_1.$log.debug("patch from email", email, "with status", status); | ||
var user = this.usersService.findByEmail(email); | ||
user.status = status; | ||
ts_log_debug_1.$log.debug("patch user", user); | ||
return this.usersService.patch(user); | ||
return this.usersService.update(user._id, { status: status }); | ||
}; | ||
/** | ||
* | ||
* @param id | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
UserCtrl.prototype.update = function (id, user) { | ||
var oldUser = this.usersService.find(id); | ||
if (!!oldUser) { | ||
if (!oldUser) { | ||
throw new ts_httpexceptions_1.NotFound("User not found."); | ||
} | ||
return this.usersService.update(Object.assign(oldUser, user)); | ||
return this.usersService.update(oldUser._id, user); | ||
}; | ||
/** | ||
* | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
UserCtrl.prototype.create = function (user) { | ||
ts_log_debug_1.$log.debug("rest create user", user); | ||
if (!user.email || !this.usersService.checkEmail(user.email)) { | ||
throw new ts_httpexceptions_1.BadRequest("Email are required"); | ||
} | ||
if (!user.password) { | ||
throw new ts_httpexceptions_1.BadRequest("Password are required"); | ||
} | ||
if (this.usersService.findByEmail(user.email)) { | ||
throw new ts_httpexceptions_1.BadRequest("User already created with this email."); | ||
} | ||
return this.usersService.create(user); | ||
}; | ||
UserCtrl.prototype.remove = function () { | ||
/** | ||
* Remove the user. | ||
* @param id | ||
* @returns {any} | ||
*/ | ||
UserCtrl.prototype.remove = function (id) { | ||
var user = this.usersService.find(id); | ||
if (!user) { | ||
throw new ts_httpexceptions_1.NotFound("User not found."); | ||
} | ||
this.usersService.remove(id); | ||
return user; | ||
}; | ||
/** | ||
* Get All users. | ||
* @returns {IUser[]} | ||
*/ | ||
UserCtrl.prototype.getList = function () { | ||
return this.usersService.queryPartial(); | ||
return this.usersService.query().map(function (o) { | ||
o = Object.assign({}, o); | ||
delete o.password; | ||
return o; | ||
}); | ||
}; | ||
@@ -74,12 +136,12 @@ return UserCtrl; | ||
__decorate([ | ||
ts_express_decorators_1.Get("/:email"), | ||
__param(0, ts_express_decorators_1.PathParams("email")), | ||
ts_express_decorators_1.Get("/:id"), | ||
__param(0, ts_express_decorators_1.PathParams("id")), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [String]), | ||
__metadata("design:returntype", Object) | ||
], UserCtrl.prototype, "getByEmail", null); | ||
], UserCtrl.prototype, "get", null); | ||
__decorate([ | ||
ts_express_decorators_1.Patch("/:email/:status"), | ||
__param(0, ts_express_decorators_1.PathParams("email")), | ||
__param(1, ts_express_decorators_1.PathParams("status")), | ||
__param(0, ts_express_decorators_1.Required()), __param(0, ts_express_decorators_1.PathParams("email")), | ||
__param(1, ts_express_decorators_1.Required()), __param(1, ts_express_decorators_1.PathParams("status")), | ||
__metadata("design:type", Function), | ||
@@ -92,3 +154,3 @@ __metadata("design:paramtypes", [String, String]), | ||
__param(0, ts_express_decorators_1.PathParams("id")), | ||
__param(1, ts_express_decorators_1.BodyParams("user")), | ||
__param(1, ts_express_decorators_1.Required()), __param(1, ts_express_decorators_1.BodyParams("user")), | ||
__metadata("design:type", Function), | ||
@@ -100,3 +162,4 @@ __metadata("design:paramtypes", [String, Object]), | ||
ts_express_decorators_1.Post("/"), | ||
__param(0, ts_express_decorators_1.BodyParams("user")), | ||
ts_express_decorators_1.Status(201), | ||
__param(0, ts_express_decorators_1.Required()), __param(0, ts_express_decorators_1.BodyParams("user")), | ||
__metadata("design:type", Function), | ||
@@ -108,4 +171,5 @@ __metadata("design:paramtypes", [Object]), | ||
ts_express_decorators_1.Delete("/:id"), | ||
__param(0, ts_express_decorators_1.PathParams("id")), | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", []), | ||
__metadata("design:paramtypes", [String]), | ||
__metadata("design:returntype", void 0) | ||
@@ -112,0 +176,0 @@ ], UserCtrl.prototype, "remove", null); |
import { IUser, PartialUser } from "../models/User"; | ||
export declare class UsersService { | ||
users: any[]; | ||
private USERS; | ||
private users; | ||
constructor(); | ||
/** | ||
* | ||
* @param email | ||
* @returns {boolean} | ||
*/ | ||
checkEmail(email: string): boolean; | ||
/** | ||
* | ||
* @param status | ||
* @returns {boolean} | ||
*/ | ||
checkStatus(status: string): boolean; | ||
/** | ||
* Find a user by his ID. | ||
@@ -23,15 +37,10 @@ * @param id | ||
query(): IUser[]; | ||
queryPartial(): PartialUser[]; | ||
/** | ||
* | ||
* @param id | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
update(user: IUser): IUser; | ||
/** | ||
* | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
patch(user: IUser): IUser; | ||
update(id: string, user: IUser | PartialUser): IUser; | ||
remove(id: string): void; | ||
} |
@@ -17,4 +17,21 @@ "use strict"; | ||
function UsersService() { | ||
this.users = this.USERS.map(function (u) { return Object.assign({}, u); }); | ||
} | ||
/** | ||
* | ||
* @param email | ||
* @returns {boolean} | ||
*/ | ||
UsersService.prototype.checkEmail = function (email) { | ||
return !!email.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/); | ||
}; | ||
/** | ||
* | ||
* @param status | ||
* @returns {boolean} | ||
*/ | ||
UsersService.prototype.checkStatus = function (status) { | ||
return ["offline", "busy", "online"].indexOf(status) !== -1; | ||
}; | ||
/** | ||
* Find a user by his ID. | ||
@@ -60,35 +77,12 @@ * @param id | ||
}; | ||
UsersService.prototype.queryPartial = function () { | ||
var partialUsers = []; | ||
this.users.forEach(function (user) { | ||
partialUsers.push({ | ||
_id: user._id, | ||
email: user.email, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
"status": "offline" | ||
}); | ||
}); | ||
return partialUsers; | ||
}; | ||
/** | ||
* | ||
* @param id | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
UsersService.prototype.update = function (user) { | ||
UsersService.prototype.update = function (id, user) { | ||
var users = this.query(); | ||
var index = this.users.findIndex(function (o) { return user._id === o._id; }); | ||
users[index] = user; | ||
return user; | ||
}; | ||
/** | ||
* | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
UsersService.prototype.patch = function (user) { | ||
var users = this.query(); | ||
ts_log_debug_1.$log.debug("users size", users.length); | ||
var index = this.users.findIndex(function (o) { return user._id === o._id; }); | ||
var index = this.users.findIndex(function (o) { return id === o._id; }); | ||
ts_log_debug_1.$log.debug("users index", index); | ||
@@ -98,2 +92,6 @@ users[index] = Object.assign(users[index], user); | ||
}; | ||
UsersService.prototype.remove = function (id) { | ||
var index = this.users.findIndex(function (o) { return id === o._id; }); | ||
this.users.splice(index, 1); | ||
}; | ||
return UsersService; | ||
@@ -104,7 +102,8 @@ }()); | ||
__metadata("design:type", Array) | ||
], UsersService.prototype, "users", void 0); | ||
], UsersService.prototype, "USERS", void 0); | ||
UsersService = __decorate([ | ||
ts_express_decorators_1.Service() | ||
ts_express_decorators_1.Service(), | ||
__metadata("design:paramtypes", []) | ||
], UsersService); | ||
exports.UsersService = UsersService; | ||
//# sourceMappingURL=UsersService.js.map |
{ | ||
"name": "labs-angular-backend", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -8,3 +8,4 @@ { | ||
"email": "johnniebenson@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -16,3 +17,4 @@ { | ||
"email": "nannieduran@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -24,3 +26,4 @@ { | ||
"email": "constancebright@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -32,3 +35,4 @@ { | ||
"email": "joleneoconnor@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -40,3 +44,4 @@ { | ||
"email": "noelmiles@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -48,3 +53,4 @@ { | ||
"email": "bestkemp@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -56,3 +62,4 @@ { | ||
"email": "kaitlindale@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -64,3 +71,4 @@ { | ||
"email": "earleneholcomb@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -72,3 +80,4 @@ { | ||
"email": "clarkholland@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -80,3 +89,4 @@ { | ||
"email": "kirstenparker@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -88,3 +98,4 @@ { | ||
"email": "hoffmankirk@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -96,3 +107,4 @@ { | ||
"email": "strongstrickland@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -104,3 +116,4 @@ { | ||
"email": "lucasmoran@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -112,3 +125,4 @@ { | ||
"email": "billieharper@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -120,3 +134,4 @@ { | ||
"email": "gambleavila@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -128,3 +143,4 @@ { | ||
"email": "casepatel@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -136,3 +152,4 @@ { | ||
"email": "kristenortega@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -144,3 +161,4 @@ { | ||
"email": "albaholloway@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -152,3 +170,4 @@ { | ||
"email": "inabass@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -160,3 +179,4 @@ { | ||
"email": "ayalajames@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -168,3 +188,4 @@ { | ||
"email": "diannevargas@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -176,3 +197,4 @@ { | ||
"email": "aureliabarnett@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -184,3 +206,4 @@ { | ||
"email": "schwartzparks@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -192,3 +215,4 @@ { | ||
"email": "antoniacasey@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -200,3 +224,4 @@ { | ||
"email": "altacurry@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -208,3 +233,4 @@ { | ||
"email": "vanessaholman@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -216,3 +242,4 @@ { | ||
"email": "lucilemoore@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -224,3 +251,4 @@ { | ||
"email": "hallwillis@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -232,3 +260,4 @@ { | ||
"email": "rosaholt@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -240,3 +269,4 @@ { | ||
"email": "latashaaustin@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -248,3 +278,4 @@ { | ||
"email": "taniaberry@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -256,3 +287,4 @@ { | ||
"email": "stacieluna@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -264,3 +296,4 @@ { | ||
"email": "bryanknight@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -272,3 +305,4 @@ { | ||
"email": "rowele@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -280,3 +314,4 @@ { | ||
"email": "finleygould@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -288,3 +323,4 @@ { | ||
"email": "milliehogan@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -296,3 +332,4 @@ { | ||
"email": "renacrane@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -304,3 +341,4 @@ { | ||
"email": "juneblack@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -312,3 +350,4 @@ { | ||
"email": "holmanweiss@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -320,3 +359,4 @@ { | ||
"email": "rosarioshaffer@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -328,3 +368,4 @@ { | ||
"email": "louisemason@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -336,3 +377,4 @@ { | ||
"email": "perezbauer@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -344,3 +386,4 @@ { | ||
"email": "huffcampos@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -352,3 +395,4 @@ { | ||
"email": "murphyestrada@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -360,3 +404,4 @@ { | ||
"email": "audreybyers@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -368,3 +413,4 @@ { | ||
"email": "vegamckee@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -376,3 +422,4 @@ { | ||
"email": "florinefrank@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -384,3 +431,4 @@ { | ||
"email": "elviaschwartz@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -392,3 +440,4 @@ { | ||
"email": "reillyblake@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -400,3 +449,4 @@ { | ||
"email": "randolphbutler@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -408,3 +458,4 @@ { | ||
"email": "daisymurphy@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -416,3 +467,4 @@ { | ||
"email": "bobbibarker@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -424,3 +476,4 @@ { | ||
"email": "betsylindsay@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -432,3 +485,4 @@ { | ||
"email": "hicksmalone@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -440,3 +494,4 @@ { | ||
"email": "yolandachristensen@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
}, | ||
@@ -448,5 +503,6 @@ { | ||
"email": "sparkskaufman@cytrex.com", | ||
"password": "12345" | ||
"password": "12345", | ||
"status": "offline" | ||
} | ||
] | ||
} |
import { | ||
Controller, Get, PathParams, Put, BodyParams, Post, Patch, Delete, Response, Required | ||
Controller, Get, PathParams, Put, BodyParams, Post, Patch, Delete, Response, Required, Status | ||
} from "ts-express-decorators"; | ||
@@ -7,3 +7,3 @@ import {$log} from "ts-log-debug"; | ||
import {IUser, PartialUser} from "../../models/User"; | ||
import {NotFound, Unauthorized} from "ts-httpexceptions"; | ||
import {NotFound, Unauthorized, BadRequest} from "ts-httpexceptions"; | ||
@@ -18,2 +18,8 @@ | ||
/** | ||
* Authenticate a user. | ||
* @param email | ||
* @param password | ||
* @returns {IUser} | ||
*/ | ||
@Post("/authenticate") | ||
@@ -27,7 +33,7 @@ public authenticate( | ||
let user: IUser = this.usersService.findByEmail(email); | ||
const user: IUser = this.usersService.findByEmail(email); | ||
$log.debug("find user by email", user); | ||
if(null == user) { | ||
if(!user) { | ||
throw new NotFound("authentication failed, user not found"); | ||
@@ -40,10 +46,22 @@ } | ||
return user; | ||
return this.usersService.update(user._id, {status: "online"}); | ||
} | ||
@Get("/:email") | ||
public getByEmail( | ||
@PathParams("email") email: string | ||
/** | ||
* Find a user by is mail or id. | ||
* @param idOrMail | ||
* @returns {IUser} | ||
*/ | ||
@Get("/:id") | ||
public get( | ||
@PathParams("id") idOrMail: string | ||
): IUser { | ||
return this.usersService.findByEmail(email); | ||
const user = this.usersService.findByEmail(idOrMail) || this.usersService.find(idOrMail) | ||
if (!user) { | ||
throw new NotFound("User not found."); | ||
} | ||
return user; | ||
} | ||
@@ -53,43 +71,108 @@ | ||
public updateStatus( | ||
@PathParams("email") email: string, | ||
@PathParams("status") status: string | ||
@Required() @PathParams("email") email: string, | ||
@Required() @PathParams("status") status: string | ||
): IUser { | ||
if (!this.usersService.checkStatus(status)) { | ||
throw new BadRequest("Wrong status"); | ||
} | ||
if (!this.usersService.checkEmail(email)) { | ||
throw new BadRequest("Wrong email"); | ||
} | ||
const user = this.usersService.findByEmail(email); | ||
if (!user) { | ||
throw new NotFound("User not found.") | ||
} | ||
$log.debug("patch from email", email, "with status", status); | ||
const user = this.usersService.findByEmail(email); | ||
user.status = status; | ||
$log.debug("patch user" ,user); | ||
return this.usersService.patch(user); | ||
return this.usersService.update(user._id, {status}); | ||
} | ||
/** | ||
* | ||
* @param id | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
@Put("/:id") | ||
public update( | ||
@PathParams("id") id: string, | ||
@BodyParams("user") user: any | ||
@Required() @BodyParams("user") user: any | ||
): IUser { | ||
const oldUser = this.usersService.find(id); | ||
if (!!oldUser) { | ||
if (!oldUser) { | ||
throw new NotFound("User not found.") | ||
} | ||
return this.usersService.update(Object.assign(oldUser, user)); | ||
return this.usersService.update(oldUser._id, user); | ||
} | ||
/** | ||
* | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
@Post("/") | ||
@Status(201) | ||
public create( | ||
@BodyParams("user") user: any | ||
@Required() @BodyParams("user") user: IUser | ||
): IUser { | ||
$log.debug("rest create user", user) | ||
$log.debug("rest create user", user); | ||
if (!user.email || !this.usersService.checkEmail(user.email)) { | ||
throw new BadRequest("Email are required"); | ||
} | ||
if (!user.password) { | ||
throw new BadRequest("Password are required"); | ||
} | ||
if (this.usersService.findByEmail(user.email)) { | ||
throw new BadRequest("User already created with this email.") | ||
} | ||
return this.usersService.create(user); | ||
} | ||
/** | ||
* Remove the user. | ||
* @param id | ||
* @returns {any} | ||
*/ | ||
@Delete("/:id") | ||
public remove() { | ||
public remove( | ||
@PathParams("id") id: string | ||
) { | ||
const user = this.usersService.find(id); | ||
if (!user) { | ||
throw new NotFound("User not found.") | ||
} | ||
this.usersService.remove(id); | ||
return user; | ||
} | ||
/** | ||
* Get All users. | ||
* @returns {IUser[]} | ||
*/ | ||
@Get("/") | ||
public getList(): PartialUser[] { | ||
return this.usersService.queryPartial(); | ||
public getList(): IUser[] { | ||
return this.usersService.query().map(o => { | ||
o = Object.assign({}, o); | ||
delete o.password; | ||
return o; | ||
}); | ||
} | ||
} |
@@ -8,6 +8,29 @@ import {Value} from "ts-json-properties"; | ||
export class UsersService { | ||
@Value("users.list") | ||
users: any[]; | ||
private USERS: any[]; | ||
private users; | ||
constructor() { | ||
this.users = this.USERS.map(u => Object.assign({}, u)); | ||
} | ||
/** | ||
* | ||
* @param email | ||
* @returns {boolean} | ||
*/ | ||
checkEmail(email: string): boolean { | ||
return !!email.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/); | ||
} | ||
/** | ||
* | ||
* @param status | ||
* @returns {boolean} | ||
*/ | ||
checkStatus(status: string): boolean { | ||
return ["offline", "busy", "online"].indexOf(status) !== -1; | ||
} | ||
/** | ||
* Find a user by his ID. | ||
@@ -57,48 +80,23 @@ * @param id | ||
public queryPartial(): PartialUser[] { | ||
let partialUsers: PartialUser[] = []; | ||
this.users.forEach( user => { | ||
partialUsers.push({ | ||
_id: user._id, | ||
email: user.email, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
"status": "offline" | ||
}); | ||
}); | ||
return partialUsers; | ||
} | ||
/** | ||
* | ||
* @param id | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
public update(user: IUser): IUser { | ||
public update(id: string, user: IUser | PartialUser): IUser { | ||
const users = this.query(); | ||
const index = this.users.findIndex(o => user._id === o._id); | ||
users[index] = user; | ||
return user; | ||
} | ||
/** | ||
* | ||
* @param user | ||
* @returns {IUser} | ||
*/ | ||
public patch(user: IUser): IUser { | ||
const users = this.query(); | ||
$log.debug("users size", users.length); | ||
const index = this.users.findIndex(o => user._id === o._id); | ||
const index = this.users.findIndex(o => id === o._id); | ||
$log.debug("users index", index); | ||
users[index] = Object.assign(users[index], user); | ||
return users[index]; | ||
} | ||
public remove(id: string) { | ||
const index = this.users.findIndex(o => id === o._id); | ||
this.users.splice(index, 1); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
88680
1802