@@ -22,14 +22,16 @@ import { Types } from "mongoose";
2222import { Roles } from "../../auth/ability/ability.factory" ;
2323import { NotificationService } from "../../notifications/notifications.service" ;
2424import slugify from "slugify" ;
25+ import { ConfigService } from "@nestjs/config" ;
2526
2627@Controller ( )
2728export class NameSpaceController {
2829 constructor (
2930 private nameSpaceService : NameSpaceService ,
3031 private usersService : UsersService ,
3132 private viewService : ViewService ,
32- private notificationService : NotificationService
33+ private notificationService : NotificationService ,
34+ private configService : ConfigService ,
3335 ) { }
3436
3537 @AdminOnly ( )
@@ -59,13 +61,14 @@ export class NameSpaceController {
5961 ...namespaceBody ,
6062 } ;
6163
62- newNameSpace . slug = slugify ( nameSpace . name , {
64+ newNameSpace . slug = slugify ( newNameSpace . name , {
6365 lower : true ,
6466 strict : true ,
6567 } ) ;
6668
6769 newNameSpace . users = await this . updateNameSpaceUsers (
6870 newNameSpace . users ,
71+ newNameSpace . slug ,
6972 nameSpace . slug
7073 ) ;
7174
@@ -100,25 +103,35 @@ export class NameSpaceController {
100103 const users = await this . usersService . findAll ( { } ) ;
101104 const parsedUrl = parse ( req . url , true ) ;
102105
103- const query = Object . assign ( parsedUrl . query , { nameSpaces, users } ) ;
106+ const query = Object . assign (
107+ parsedUrl . query ,
108+ {
109+ sitekey : this . configService . get < string > ( "recaptcha_sitekey" ) ,
110+ nameSpaces,
111+ users
112+ }
113+ ) ;
104114
105115 await this . viewService . render ( req , res , "/admin-namespaces" , query ) ;
106116 }
107117
108- private async updateNameSpaceUsers ( users , key ) {
118+ private async updateNameSpaceUsers ( users , newKey , oldKey = null ) {
109119 const promises = users . map ( async ( user ) => {
110120 const userId = Types . ObjectId ( user . _id ) ;
111121 const existingUser = await this . usersService . getById ( userId ) ;
112122
113- if ( ! existingUser . role [ key ] ) {
114- await this . usersService . updateUser ( existingUser . _id , {
115- role : {
116- ...existingUser . role ,
117- [ key ] : Roles . Regular ,
118- } ,
119- } ) ;
123+ let updatedRoles = { ...existingUser . role } ;
124+
125+ if ( oldKey && oldKey !== newKey ) {
126+ delete updatedRoles [ oldKey ] ;
120127 }
121128
129+ updatedRoles [ newKey ] = Roles . Regular ;
130+
131+ await this . usersService . updateUser ( existingUser . _id , {
132+ role : updatedRoles ,
133+ } ) ;
134+
122135 return userId ;
123136 } ) ;
124137
0 commit comments