252 lines
11 KiB
JavaScript
252 lines
11 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.UsersController = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const class_validator_1 = require("class-validator");
|
|
const users_service_1 = require("./users.service");
|
|
const current_user_decorator_1 = require("../auth/decorators/current-user.decorator");
|
|
const roles_decorator_1 = require("../auth/decorators/roles.decorator");
|
|
class SetRoleDto {
|
|
}
|
|
__decorate([
|
|
(0, class_validator_1.IsIn)(['admin', 'user']),
|
|
__metadata("design:type", String)
|
|
], SetRoleDto.prototype, "role", void 0);
|
|
class SetPremiumDto {
|
|
}
|
|
__decorate([
|
|
(0, class_validator_1.IsBoolean)(),
|
|
__metadata("design:type", Boolean)
|
|
], SetPremiumDto.prototype, "isPremium", void 0);
|
|
class SetRecipeSharingDto {
|
|
}
|
|
__decorate([
|
|
(0, class_validator_1.IsBoolean)(),
|
|
__metadata("design:type", Boolean)
|
|
], SetRecipeSharingDto.prototype, "canShareRecipes", void 0);
|
|
class AdminCreateUserDto {
|
|
}
|
|
__decorate([
|
|
(0, class_validator_1.IsString)(),
|
|
(0, class_validator_1.MinLength)(2),
|
|
(0, class_validator_1.MaxLength)(50),
|
|
__metadata("design:type", String)
|
|
], AdminCreateUserDto.prototype, "username", void 0);
|
|
__decorate([
|
|
(0, class_validator_1.IsEmail)(),
|
|
__metadata("design:type", String)
|
|
], AdminCreateUserDto.prototype, "email", void 0);
|
|
__decorate([
|
|
(0, class_validator_1.IsString)(),
|
|
(0, class_validator_1.MinLength)(8),
|
|
__metadata("design:type", String)
|
|
], AdminCreateUserDto.prototype, "password", void 0);
|
|
__decorate([
|
|
(0, class_validator_1.IsOptional)(),
|
|
(0, class_validator_1.IsIn)(['admin', 'user']),
|
|
__metadata("design:type", String)
|
|
], AdminCreateUserDto.prototype, "role", void 0);
|
|
class UpdateEmailDto {
|
|
}
|
|
__decorate([
|
|
(0, class_validator_1.IsEmail)(),
|
|
__metadata("design:type", String)
|
|
], UpdateEmailDto.prototype, "email", void 0);
|
|
class UpdateProfileDto {
|
|
}
|
|
__decorate([
|
|
(0, class_validator_1.IsOptional)(),
|
|
(0, class_validator_1.IsString)(),
|
|
(0, class_validator_1.MaxLength)(100),
|
|
__metadata("design:type", String)
|
|
], UpdateProfileDto.prototype, "firstName", void 0);
|
|
__decorate([
|
|
(0, class_validator_1.IsOptional)(),
|
|
(0, class_validator_1.IsString)(),
|
|
(0, class_validator_1.MaxLength)(100),
|
|
__metadata("design:type", String)
|
|
], UpdateProfileDto.prototype, "lastName", void 0);
|
|
__decorate([
|
|
(0, class_validator_1.IsOptional)(),
|
|
(0, class_validator_1.IsEmail)(),
|
|
__metadata("design:type", String)
|
|
], UpdateProfileDto.prototype, "email", void 0);
|
|
let UsersController = class UsersController {
|
|
constructor(usersService) {
|
|
this.usersService = usersService;
|
|
}
|
|
async getMe(user) {
|
|
const found = await this.usersService.findById(user.userId);
|
|
return {
|
|
id: found?.id,
|
|
username: found?.username,
|
|
email: found?.email,
|
|
firstName: found?.firstName,
|
|
lastName: found?.lastName,
|
|
role: found?.role,
|
|
};
|
|
}
|
|
async updateMe(user, dto) {
|
|
const updated = await this.usersService.updateProfile(user.userId, dto);
|
|
return {
|
|
id: updated.id,
|
|
username: updated.username,
|
|
email: updated.email,
|
|
firstName: updated.firstName,
|
|
lastName: updated.lastName,
|
|
};
|
|
}
|
|
listUsers() {
|
|
return this.usersService.findAll();
|
|
}
|
|
async setRole(id, caller, dto) {
|
|
if (caller.userId === id)
|
|
throw new common_1.BadRequestException('Du kan inte ändra din egen roll');
|
|
const updated = await this.usersService.setRole(id, dto.role);
|
|
return { id: updated.id, username: updated.username, role: updated.role };
|
|
}
|
|
async setPremium(id, dto) {
|
|
const updated = await this.usersService.setPremium(id, dto.isPremium);
|
|
return { id: updated.id, username: updated.username, isPremium: updated.isPremium };
|
|
}
|
|
async setRecipeSharing(id, dto) {
|
|
const updated = await this.usersService.setRecipeSharing(id, dto.canShareRecipes);
|
|
return { id: updated.id, username: updated.username, canShareRecipes: updated.canShareRecipes };
|
|
}
|
|
async adminCreateUser(dto) {
|
|
const user = await this.usersService.adminCreate(dto);
|
|
return { id: user.id, username: user.username, email: user.email, role: user.role, createdAt: user.createdAt };
|
|
}
|
|
async deleteUser(id, caller) {
|
|
if (caller.userId === id)
|
|
throw new common_1.BadRequestException('Du kan inte ta bort ditt eget konto');
|
|
await this.usersService.deleteUser(id);
|
|
return { deleted: true };
|
|
}
|
|
async resetPassword(id, caller) {
|
|
if (caller.userId === id)
|
|
throw new common_1.BadRequestException('Du kan inte återställa ditt eget lösenord härifrån');
|
|
const user = await this.usersService.findById(id);
|
|
if (!user)
|
|
throw new common_1.BadRequestException('Användaren hittades inte');
|
|
const { temporaryPassword } = await this.usersService.resetPassword(id);
|
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL ?? 'appen';
|
|
const displayName = user.firstName ? user.firstName : user.username;
|
|
return {
|
|
to: user.email,
|
|
subject: 'Ditt lösenord har återställts',
|
|
body: `Hej ${displayName},\n\nDitt lösenord har återställts av en administratör.\nDitt nya tillôlliga lösenord är: ${temporaryPassword}\n\nLogga in på ${appUrl} och byt lösenord snarast.\n\nHälsningar`,
|
|
temporaryPassword,
|
|
};
|
|
}
|
|
async updateEmail(id, caller, dto) {
|
|
if (caller.userId === id)
|
|
throw new common_1.BadRequestException('Använd "Min profil" för att ändra din egen e-post');
|
|
const updated = await this.usersService.updateEmail(id, dto.email);
|
|
return { id: updated.id, username: updated.username, email: updated.email };
|
|
}
|
|
};
|
|
exports.UsersController = UsersController;
|
|
__decorate([
|
|
(0, common_1.Get)('me'),
|
|
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "getMe", null);
|
|
__decorate([
|
|
(0, common_1.Patch)('me'),
|
|
__param(0, (0, current_user_decorator_1.CurrentUser)()),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object, UpdateProfileDto]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "updateMe", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Get)(),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", void 0)
|
|
], UsersController.prototype, "listUsers", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id/role'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, current_user_decorator_1.CurrentUser)()),
|
|
__param(2, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object, SetRoleDto]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "setRole", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id/premium'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, SetPremiumDto]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "setPremium", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id/recipe-sharing'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, SetRecipeSharingDto]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "setRecipeSharing", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)(),
|
|
__param(0, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [AdminCreateUserDto]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "adminCreateUser", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Delete)(':id'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, current_user_decorator_1.CurrentUser)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "deleteUser", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Post)(':id/reset-password'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, current_user_decorator_1.CurrentUser)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "resetPassword", null);
|
|
__decorate([
|
|
(0, roles_decorator_1.Roles)('admin'),
|
|
(0, common_1.Patch)(':id/email'),
|
|
__param(0, (0, common_1.Param)('id', common_1.ParseIntPipe)),
|
|
__param(1, (0, current_user_decorator_1.CurrentUser)()),
|
|
__param(2, (0, common_1.Body)()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number, Object, UpdateEmailDto]),
|
|
__metadata("design:returntype", Promise)
|
|
], UsersController.prototype, "updateEmail", null);
|
|
exports.UsersController = UsersController = __decorate([
|
|
(0, common_1.Controller)('users'),
|
|
__metadata("design:paramtypes", [users_service_1.UsersService])
|
|
], UsersController);
|
|
//# sourceMappingURL=users.controller.js.map
|