Skip to content
Merged
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
120 changes: 80 additions & 40 deletions src/modules/identity/repositories/team.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export class TeamRepository {
name: team.name,
hubUrl: team.hubUrl,
logo: team.logo,
workspaces:[],
workspaces: [],
isRestricted: true,
users:team.users,
owner:team.owner
users: team.users,
owner: team.owner,
} as unknown as WithId<Team>;
}
// Case 2: Plan active → filter workspaces
Expand Down Expand Up @@ -141,10 +141,10 @@ export class TeamRepository {
name: team.name,
hubUrl: team.hubUrl,
logo: team.logo,
workspaces:[],
workspaces: [],
isRestricted: true,
users:team.users,
owner:team.owner
users: team.users,
owner: team.owner,
} as Partial<WithId<Team>>;
}
// Filter out restricted workspaces
Expand Down Expand Up @@ -239,7 +239,6 @@ export class TeamRepository {
return responseData.value;
}


async updateTeamWorkspaceCountById(
id: ObjectId,
planData: PlanDto,
Expand All @@ -249,9 +248,22 @@ export class TeamRepository {
.collection<Team>(Collections.TEAM)
.findOneAndUpdate(
{
_id: id, $expr: {
$lt: [{ $size: "$workspaces" }, planData.limits.workspacesPerHub.value],
} ,
_id: id,
$expr: {
$lt: [
{
$size: {
$filter: {
input: "$workspaces",
as: "workspace",
// keep only those where isRestricted is not true
cond: { $ne: ["$$workspace.isRestricted", true] },
},
},
},
planData.limits.workspacesPerHub.value,
],
},
},
{
$push: { workspaces: ws },
Expand Down Expand Up @@ -313,11 +325,16 @@ export class TeamRepository {
.updateOne({ _id }, { $set: { isHubTrialExhausted, plan } });
}

async hubCollaboratorLimitCheck(teamId: string, users: Invite[]): Promise<WithId<Team>> {
async hubCollaboratorLimitCheck(
teamId: string,
users: Invite[],
): Promise<WithId<Team>> {
const teamObjectId = new ObjectId(teamId);
const incomingEmails = users.map(u => u.email);
const incomingEmails = users.map((u) => u.email);

const result = await this.db.collection<Team>(Collections.TEAM).findOneAndUpdate(
const result = await this.db
.collection<Team>(Collections.TEAM)
.findOneAndUpdate(
{
_id: teamObjectId,
// Ensure limit not exceeded
Expand All @@ -333,18 +350,30 @@ export class TeamRepository {
incomingEmails,
{
$concatArrays: [
{ $map: { input: { $ifNull: ["$users", []] }, as: "u", in: "$$u.email" } },
{ $map: { input: { $ifNull: ["$invites", []] }, as: "i", in: "$$i.email" } }
]
}
]
}
}
]
{
$map: {
input: { $ifNull: ["$users", []] },
as: "u",
in: "$$u.email",
},
},
{
$map: {
input: { $ifNull: ["$invites", []] },
as: "i",
in: "$$i.email",
},
},
],
},
],
},
},
],
},
{ $add: ["$plan.limits.usersPerHub.value", 1] }
]
}
{ $add: ["$plan.limits.usersPerHub.value", 1] },
],
},
},
[
{
Expand All @@ -362,24 +391,35 @@ export class TeamRepository {
"$$newInvite.email",
{
$concatArrays: [
{ $map: { input: { $ifNull: ["$users", []] }, as: "u", in: "$$u.email" } },
{ $map: { input: { $ifNull: ["$invites", []] }, as: "i", in: "$$i.email" } }
]
}
]
}
}
}
}
]
}
}
}
] ,
{ returnDocument: "after" }
{
$map: {
input: { $ifNull: ["$users", []] },
as: "u",
in: "$$u.email",
},
},
{
$map: {
input: { $ifNull: ["$invites", []] },
as: "i",
in: "$$i.email",
},
},
],
},
],
},
},
},
},
],
},
},
},
],
{ returnDocument: "after" },
);

return result.value;
}

}
Loading