Skip to content

Commit

Permalink
Moved namespace logic that is depedent of the user-service to the nam…
Browse files Browse the repository at this point in the history
…espace-controller file
  • Loading branch information
pepermao committed Jul 18, 2024
1 parent 779617f commit 15ebea4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 97 deletions.
5 changes: 5 additions & 0 deletions server/auth/name-space/dto/create-namespace.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ export class CreateNameSpaceDTO {
@IsOptional()
@ApiProperty()
users: User[];

@IsString()
@IsOptional()
@ApiProperty()
slug: string;
}
98 changes: 95 additions & 3 deletions server/auth/name-space/name-space.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,67 @@ import {
CheckAbilities,
} from "../../auth/ability/ability.decorator";
import { AbilitiesGuard } from "../../auth/ability/abilities.guard";
import { Types } from "mongoose";
import { Roles } from "../../auth/ability/ability.factory";
import { NotificationService } from "../../notifications/notifications.service";
import slugify from "slugify";

@Controller()
export class NameSpaceController {
constructor(
private nameSpaceService: NameSpaceService,
private usersService: UsersService,
private viewService: ViewService
private viewService: ViewService,
private notificationService: NotificationService
) {}

@ApiTags("name-space")
@Post("api/name-space")
@UseGuards(AbilitiesGuard)
@CheckAbilities(new AdminUserAbility())
async create(@Body() namespace: CreateNameSpaceDTO) {
namespace.slug = slugify(namespace.name, {
lower: true,
strict: true,
});

namespace.users = await this.updateNameSpaceUsers(
namespace.users,
namespace.slug
);

return await this.nameSpaceService.create(namespace);
}

@ApiTags("name-space")
@Put("api/name-space/:id")
@UseGuards(AbilitiesGuard)
@CheckAbilities(new AdminUserAbility())
async update(@Param("id") id, @Body() namespace: UpdateNameSpaceDTO) {
return await this.nameSpaceService.update(id, namespace);
async update(@Param("id") id, @Body() namespaceBody: UpdateNameSpaceDTO) {
const nameSpace = await this.nameSpaceService.getById(id);
const newNameSpace = {
...nameSpace.toObject(),
...namespaceBody,
};

newNameSpace.slug = slugify(nameSpace.name, {
lower: true,
strict: true,
});

newNameSpace.users = await this.updateNameSpaceUsers(
newNameSpace.users,
nameSpace.slug
);

await this.findNameSpaceUsersAndDelete(
id,
nameSpace.slug,
newNameSpace.users,
nameSpace.users
);

return await this.nameSpaceService.update(id, newNameSpace);
}

@ApiTags("name-space")
Expand All @@ -62,4 +100,58 @@ export class NameSpaceController {
Object.assign(parsedUrl.query, { nameSpaces, users })
);
}

private async updateNameSpaceUsers(users, key) {
const promises = users.map(async (user) => {
const userId = Types.ObjectId(user._id);
const existingUser = await this.usersService.getById(userId);

if (!existingUser.role[key]) {
await this.usersService.updateUser(existingUser._id, {
role: {
...existingUser.role,
[key]: Roles.Regular,
},
});
}

return userId;
});

return await Promise.all(promises);
}

private async findNameSpaceUsersAndDelete(
id,
nameSpaceSlug,
users,
previousUsersId
) {
const usersIdSet = new Set(users.map((user) => user.toString()));
const nameSpaceUsersTodelete = previousUsersId.filter(
(previousUserId) => !usersIdSet.has(previousUserId.toString())
);

if (nameSpaceUsersTodelete.length > 0) {
this.notificationService.removeTopicSubscriber(
id,
nameSpaceUsersTodelete
);
return await this.deleteUsersNameSpace(
nameSpaceUsersTodelete,
nameSpaceSlug
);
}
}

private async deleteUsersNameSpace(usersId, key) {
const updatePromises = usersId.map(async (userId) => {
const id = Types.ObjectId(userId);
const user = await this.usersService.getById(id);
delete user.role[key];
return this.usersService.updateUser(user._id, { role: user.role });
});

await Promise.all(updatePromises);
}
}
98 changes: 4 additions & 94 deletions server/auth/name-space/name-space.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Injectable } from "@nestjs/common";
import { Model, Types } from "mongoose";
import { Model } from "mongoose";
import { NameSpaceDocument, NameSpace } from "./schemas/name-space.schema";
import { InjectModel } from "@nestjs/mongoose";
import slugify from "slugify";
import { UsersService } from "../../users/users.service";
import { Roles } from "../../auth/ability/ability.factory";
import { UpdateNameSpaceDTO } from "./dto/update-name-space.dto";
import { NotificationService } from "../../notifications/notifications.service";

Expand All @@ -13,25 +10,14 @@ export class NameSpaceService {
constructor(
@InjectModel(NameSpace.name)
private NameSpaceModel: Model<NameSpaceDocument>,
private notificationService: NotificationService,
private usersService: UsersService
private notificationService: NotificationService
) {}

listAll() {
return this.NameSpaceModel.find().populate("users");
}

async create(nameSpace) {
nameSpace.slug = slugify(nameSpace.name, {
lower: true,
strict: true,
});

nameSpace.users = await this.updateNameSpaceUsers(
nameSpace.users,
nameSpace.slug
);

const newNameSpace = await new this.NameSpaceModel(nameSpace).save();

await this.notificationService.createTopic(
Expand All @@ -47,43 +33,20 @@ export class NameSpaceService {
return newNameSpace;
}

async update(id, nameSpaceBody: UpdateNameSpaceDTO) {
const nameSpace = await this.getById(id);
const newNameSpace = {
...nameSpace.toObject(),
...nameSpaceBody,
};

newNameSpace.slug = slugify(newNameSpace.name, {
lower: true,
strict: true,
});

async update(id, newNameSpace: UpdateNameSpaceDTO) {
const isNameSpaceTopic = await this.notificationService.getTopic(
newNameSpace._id
);

newNameSpace.users = await this.updateNameSpaceUsers(
newNameSpace.users,
nameSpace.slug
);

await this.ensureTopicAndSubscribers(
id,
newNameSpace.name,
newNameSpace.users,
isNameSpaceTopic
);

await this.findNameSpaceUsersAndDelete(
id,
nameSpace.slug,
newNameSpace.users,
nameSpace.users
);

return await this.NameSpaceModel.updateOne(
{ _id: nameSpace._id },
{ _id: newNameSpace._id },
newNameSpace
);
}
Expand All @@ -103,59 +66,6 @@ export class NameSpaceService {
await this.notificationService.addTopicSubscriber(namespaceId, users);
}

async updateNameSpaceUsers(users, key) {
const promises = users.map(async (user) => {
const userId = Types.ObjectId(user._id);
const existingUser = await this.usersService.getById(userId);

if (!user.role[key]) {
await this.usersService.updateUser(existingUser._id, {
role: {
...existingUser.role,
[key]: Roles.Regular,
},
});
}

return userId;
});

return await Promise.all(promises);
}

async deleteUsersNameSpace(usersId, key) {
const updatePromises = usersId.map(async (userId) => {
const id = Types.ObjectId(userId);
const user = await this.usersService.getById(id);
delete user.role[key];
return this.usersService.updateUser(user._id, { role: user.role });
});

await Promise.all(updatePromises);
}

async findNameSpaceUsersAndDelete(
id,
nameSpaceSlug,
users,
previousUsersId
) {
const usersIdSet = new Set(users.map((user) => user.toString()));
const nameSpaceUsersTodelete = previousUsersId.filter(
(previousUserId) => !usersIdSet.has(previousUserId.toString())
);
if (nameSpaceUsersTodelete.length > 0) {
this.notificationService.removeTopicSubscriber(
id,
nameSpaceUsersTodelete
);
return await this.deleteUsersNameSpace(
nameSpaceUsersTodelete,
nameSpaceSlug
);
}
}

findOne(match) {
return this.NameSpaceModel.findOne(match);
}
Expand Down

0 comments on commit 15ebea4

Please sign in to comment.