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

Added status for invited users #211

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/common/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class EmailService {
async sendInviteUserEmail(
email: string,
ownerEmail: string,
kbName: string,
kbId: string,
userExist: boolean,
) {
if (!this.isSgInitialized) return;
Expand All @@ -131,14 +131,16 @@ export class EmailService {
? `${this.clientUrl}/login`
: `${this.clientUrl}/sign-up`;

const acceptUrl = `${this.clientUrl}/knowledgebase/kbId/accept_invite`;

const msg = {
to: email,
from: { email: this.senderEmail, name: this.senderName },
templateId: 'd-46f484fb64ae432aa8ca732e5ab2c1a9',
dynamicTemplateData: {
owner_email: ownerEmail,
user_name: ownerEmail,
kb_name: kbName,
accept_url: acceptUrl,
website_url: websiteUrl,
},
};
Expand Down
11 changes: 11 additions & 0 deletions src/knowledgebase/knowledgebase-db.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class KnowledgebaseDbService {
{
$match: {
'participants.id': userId,
'participants.status': 'Active',
},
},
{
Expand Down Expand Up @@ -687,4 +688,14 @@ export class KnowledgebaseDbService {
async deletePrompt(id: ObjectId) {
await this.promptCollection.deleteOne({ _id: id });
}

async getAllTheParticipatedKnowledgeBaseForUser(userId: ObjectId) {
const query = { 'participants.id': userId };
const projection = { name: 1, 'participants.$': 1 };
const result = await this.knowledgebaseCollection
.find(query)
.project(projection)
.toArray();
return result;
}
}
5 changes: 5 additions & 0 deletions src/knowledgebase/knowledgebase-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export enum UserPermissions {
DELETE_USER = 'delete_user',
}

export enum InviteStatus {
PENDING = 'Pending',
ACTIVE = 'Active',
}

const rolePermissions = {
[UserRoles.READER]: [UserPermissions.READ],
[UserRoles.EDITOR]: [UserPermissions.READ, UserPermissions.EDIT],
Expand Down
13 changes: 13 additions & 0 deletions src/knowledgebase/knowledgebase.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ export class KnowledgebaseController {
return this.kbService.createKnowledgebase(user, data);
}

@Get('/participant/list')
async getKnowledgeBasesForParticipants(@Req() req: RequestWithUser) {
const { user } = req;
return this.kbService.getKnowledgeBasesForParticipants(user);
}

@Post('/:id/invite_user')
async inviteUser(
@Req() req: RequestWithUser,
Expand All @@ -347,4 +353,11 @@ export class KnowledgebaseController {

return this.kbService.deleteUserFromKnowledgeBase(user, kbId, userId);
}

@Get('/:id/accept_invite')
async acceptInvite(@Req() req: RequestWithUser, @Param('id') id: string) {
const { user } = req;

return this.kbService.inviteUserAccept(user, id);
}
}
1 change: 1 addition & 0 deletions src/knowledgebase/knowledgebase.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface ParticipantsData {
id: ObjectId;
role: UserRoles;
email: string;
status: string;
}

export interface Knowledgebase {
Expand Down
66 changes: 63 additions & 3 deletions src/knowledgebase/knowledgebase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { KnowledgebaseDbService } from './knowledgebase-db.service';
import {
checkUserPermissionForKb,
UserPermissions,
InviteStatus,
} from './knowledgebase-utils';
import {
CreateKnowledgebaseDTO,
Expand Down Expand Up @@ -163,6 +164,7 @@ export class KnowledgebaseService {
id: user._id,
role: UserRoles.ADMIN,
email: user.email,
status: InviteStatus.ACTIVE,
};
// Create a new Kb in db
const ts = new Date();
Expand Down Expand Up @@ -347,6 +349,21 @@ export class KnowledgebaseService {
return kbs;
}

/**
* Get all knowledgebases for a participant
* @param user
* @returns
*/
async getKnowledgeBasesForParticipants(user: UserSparse) {
// Fetch all the knowledgebase for a user who participates
const kbs =
await this.kbDbService.getAllTheParticipatedKnowledgeBaseForUser(
user._id,
);

return kbs;
}

/**
* Get knowledgebase detail data
* @param user
Expand Down Expand Up @@ -602,6 +619,7 @@ export class KnowledgebaseService {
kbId: ObjectId,
role: string,
email: string,
status: string,
kb,
) {
let updatedParticipants;
Expand All @@ -614,6 +632,7 @@ export class KnowledgebaseService {
id: userId,
role: role,
email: email,
status: status,
};
}
return owner;
Expand All @@ -626,10 +645,17 @@ export class KnowledgebaseService {
)
) {
// Add the new invitation for the user
updatedParticipants.push({ id: userId, role: role, email: email });
updatedParticipants.push({
id: userId,
role: role,
email: email,
status: status,
});
}
} else {
updatedParticipants = [{ id: userId, role: role, email: email }];
updatedParticipants = [
{ id: userId, role: role, email: email, status: status },
];
}

if (updatedParticipants) {
Expand Down Expand Up @@ -668,6 +694,7 @@ export class KnowledgebaseService {
kbId,
data.role,
data.email,
InviteStatus.PENDING,
kb,
);
userExist = true;
Expand All @@ -684,7 +711,7 @@ export class KnowledgebaseService {
await this.emailService.sendInviteUserEmail(
data.email,
user.email,
kb.name,
kb._id.toString(),
userExist,
);

Expand All @@ -705,6 +732,7 @@ export class KnowledgebaseService {
kb._id,
invitedData.role,
email,
InviteStatus.PENDING,
kb,
);
await this.userService.deleteFromInvitedEmail(email, kb._id);
Expand Down Expand Up @@ -741,4 +769,36 @@ export class KnowledgebaseService {

return;
}

async inviteUserAccept(user: UserSparse, id: string) {
const kbId = new ObjectId(id);
const kb = await this.kbDbService.getKnowledgebaseSparseById(kbId);

if (user._id.toString() === kb.owner.toString()) {
throw new HttpException(
'knowledgebase owner cannot accept the invite request!',
HttpStatus.BAD_REQUEST,
);
}

let updatedParticipants;
if (kb.participants && Array.isArray(kb.participants)) {
updatedParticipants = kb.participants.map((owner) => {
if (owner.id.toString() === user._id.toString()) {
// Update the existing invitation for the user
owner.status = InviteStatus.ACTIVE;
}
return owner;
});
if (updatedParticipants) {
await this.kbDbService.updateKnowledgebaseParticipants(
kbId,
updatedParticipants,
);
}
} else {
throw new HttpException('Invalid request!', HttpStatus.BAD_REQUEST);
}
return;
}
}