feat: v2 membership attributes (#21131)
* refactor: getByUserId.handler.ts * feat: return org membership attributes * docs * export new library function * test: attributes * chore: bump libraries * refactor: value -> option
This commit is contained in:
+196
-10
@@ -6,6 +6,7 @@ import { CreateOrgMembershipOutput } from "@/modules/organizations/memberships/o
|
||||
import { DeleteOrgMembership } from "@/modules/organizations/memberships/outputs/delete-membership.output";
|
||||
import { GetAllOrgMemberships } from "@/modules/organizations/memberships/outputs/get-all-memberships.output";
|
||||
import { GetOrgMembership } from "@/modules/organizations/memberships/outputs/get-membership.output";
|
||||
import { OrgUserAttribute } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { UpdateOrgMembership } from "@/modules/organizations/memberships/outputs/update-membership.output";
|
||||
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
@@ -14,17 +15,16 @@ import { UsersModule } from "@/modules/users/users.module";
|
||||
import { INestApplication } from "@nestjs/common";
|
||||
import { NestExpressApplication } from "@nestjs/platform-express";
|
||||
import { Test } from "@nestjs/testing";
|
||||
import { User } from "@prisma/client";
|
||||
import { Attribute, AttributeOption, User } from "@prisma/client";
|
||||
import * as request from "supertest";
|
||||
import { AttributeRepositoryFixture } from "test/fixtures/repository/attributes.repository.fixture";
|
||||
import { MembershipRepositoryFixture } from "test/fixtures/repository/membership.repository.fixture";
|
||||
import { OrganizationRepositoryFixture } from "test/fixtures/repository/organization.repository.fixture";
|
||||
import { TeamRepositoryFixture } from "test/fixtures/repository/team.repository.fixture";
|
||||
import { UserRepositoryFixture } from "test/fixtures/repository/users.repository.fixture";
|
||||
import { randomString } from "test/utils/randomString";
|
||||
import { withApiAuth } from "test/utils/withApiAuth";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import { ApiSuccessResponse } from "@calcom/platform-types";
|
||||
import { Membership, Team } from "@calcom/prisma/client";
|
||||
|
||||
describe("Organizations Memberships Endpoints", () => {
|
||||
@@ -33,7 +33,8 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
|
||||
let userRepositoryFixture: UserRepositoryFixture;
|
||||
let organizationsRepositoryFixture: OrganizationRepositoryFixture;
|
||||
let membershipsRepositoryFixture: MembershipRepositoryFixture;
|
||||
let membershipRepositoryFixture: MembershipRepositoryFixture;
|
||||
let attributesRepositoryFixture: AttributeRepositoryFixture;
|
||||
|
||||
let org: Team;
|
||||
let membership: Membership;
|
||||
@@ -49,6 +50,17 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
|
||||
let userToInviteViaApi: User;
|
||||
|
||||
let textAttribute: Attribute;
|
||||
let multiSelectAttribute: Attribute;
|
||||
let numberAttribute: Attribute;
|
||||
let singleSelectAttribute: Attribute;
|
||||
|
||||
let textAttributeOption: AttributeOption;
|
||||
let multiSelectAttributeOption: AttributeOption;
|
||||
let multiSelectAttributeOption2: AttributeOption;
|
||||
let numberAttributeOption: AttributeOption;
|
||||
let singleSelectAttributeOption: AttributeOption;
|
||||
|
||||
const metadata = {
|
||||
some: "key",
|
||||
};
|
||||
@@ -64,7 +76,8 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
|
||||
userRepositoryFixture = new UserRepositoryFixture(moduleRef);
|
||||
organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
|
||||
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
|
||||
membershipRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
|
||||
attributesRepositoryFixture = new AttributeRepositoryFixture(moduleRef);
|
||||
|
||||
user = await userRepositoryFixture.create({
|
||||
email: userEmail,
|
||||
@@ -91,16 +104,42 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
isOrganization: true,
|
||||
});
|
||||
|
||||
membership = await membershipsRepositoryFixture.create({
|
||||
await setupAttributes();
|
||||
|
||||
membership = await membershipRepositoryFixture.create({
|
||||
role: "ADMIN",
|
||||
user: { connect: { id: user.id } },
|
||||
team: { connect: { id: org.id } },
|
||||
});
|
||||
|
||||
membership2 = await membershipsRepositoryFixture.create({
|
||||
membership2 = await membershipRepositoryFixture.create({
|
||||
role: "MEMBER",
|
||||
user: { connect: { id: user2.id } },
|
||||
team: { connect: { id: org.id } },
|
||||
AttributeToUser: {
|
||||
create: [
|
||||
{
|
||||
attributeOption: { connect: { id: textAttributeOption.id } },
|
||||
weight: 100,
|
||||
},
|
||||
{
|
||||
attributeOption: { connect: { id: multiSelectAttributeOption.id } },
|
||||
weight: 100,
|
||||
},
|
||||
{
|
||||
attributeOption: { connect: { id: multiSelectAttributeOption2.id } },
|
||||
weight: null,
|
||||
},
|
||||
{
|
||||
attributeOption: { connect: { id: numberAttributeOption.id } },
|
||||
weight: 100,
|
||||
},
|
||||
{
|
||||
attributeOption: { connect: { id: singleSelectAttributeOption.id } },
|
||||
weight: 100,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
app = moduleRef.createNestApplication();
|
||||
@@ -109,6 +148,87 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
await app.init();
|
||||
});
|
||||
|
||||
async function setupAttributes() {
|
||||
textAttribute = await attributesRepositoryFixture.create({
|
||||
team: { connect: { id: org.id } },
|
||||
type: "TEXT",
|
||||
name: "team",
|
||||
slug: `team-${randomString()}`,
|
||||
enabled: true,
|
||||
usersCanEditRelation: false,
|
||||
isWeightsEnabled: false,
|
||||
isLocked: false,
|
||||
});
|
||||
|
||||
multiSelectAttribute = await attributesRepositoryFixture.create({
|
||||
team: { connect: { id: org.id } },
|
||||
type: "MULTI_SELECT",
|
||||
name: "skills",
|
||||
slug: `skills-${randomString()}`,
|
||||
enabled: true,
|
||||
usersCanEditRelation: false,
|
||||
isWeightsEnabled: false,
|
||||
isLocked: false,
|
||||
});
|
||||
|
||||
numberAttribute = await attributesRepositoryFixture.create({
|
||||
team: { connect: { id: org.id } },
|
||||
type: "NUMBER",
|
||||
name: "age",
|
||||
slug: `age-${randomString()}`,
|
||||
enabled: true,
|
||||
usersCanEditRelation: false,
|
||||
isWeightsEnabled: false,
|
||||
isLocked: false,
|
||||
});
|
||||
|
||||
singleSelectAttribute = await attributesRepositoryFixture.create({
|
||||
team: { connect: { id: org.id } },
|
||||
type: "SINGLE_SELECT",
|
||||
name: "frontend",
|
||||
slug: `frontend-${randomString()}`,
|
||||
enabled: true,
|
||||
usersCanEditRelation: false,
|
||||
isWeightsEnabled: false,
|
||||
isLocked: false,
|
||||
});
|
||||
|
||||
textAttributeOption = await attributesRepositoryFixture.createOption({
|
||||
attribute: { connect: { id: textAttribute.id } },
|
||||
value: "coders",
|
||||
slug: "coders",
|
||||
isGroup: false,
|
||||
});
|
||||
|
||||
multiSelectAttributeOption = await attributesRepositoryFixture.createOption({
|
||||
attribute: { connect: { id: multiSelectAttribute.id } },
|
||||
value: "javascript",
|
||||
slug: "javascript",
|
||||
isGroup: false,
|
||||
});
|
||||
|
||||
multiSelectAttributeOption2 = await attributesRepositoryFixture.createOption({
|
||||
attribute: { connect: { id: multiSelectAttribute.id } },
|
||||
value: "typescript",
|
||||
slug: "typescript",
|
||||
isGroup: false,
|
||||
});
|
||||
|
||||
numberAttributeOption = await attributesRepositoryFixture.createOption({
|
||||
attribute: { connect: { id: numberAttribute.id } },
|
||||
value: "18",
|
||||
slug: "18",
|
||||
isGroup: false,
|
||||
});
|
||||
|
||||
singleSelectAttributeOption = await attributesRepositoryFixture.createOption({
|
||||
attribute: { connect: { id: singleSelectAttribute.id } },
|
||||
value: "yes",
|
||||
slug: "yes",
|
||||
isGroup: false,
|
||||
});
|
||||
}
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(userRepositoryFixture).toBeDefined();
|
||||
expect(organizationsRepositoryFixture).toBeDefined();
|
||||
@@ -139,9 +259,54 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
expect(responseBody.data[1].user.email).toEqual(user2.email);
|
||||
expect(responseBody.data[1].user.username).toEqual(user2.username);
|
||||
expect(responseBody.data[1].teamId).toEqual(org.id);
|
||||
userHasCorrectAttributes(responseBody.data[1].attributes);
|
||||
});
|
||||
});
|
||||
|
||||
function userHasCorrectAttributes(attributes: OrgUserAttribute[]) {
|
||||
expect(attributes.length).toEqual(4);
|
||||
const responseNumberAttribute = attributes.find((attr) => attr.type === "number");
|
||||
const responseSingleSelectAttribute = attributes.find((attr) => attr.type === "singleSelect");
|
||||
const responseMultiSelectAttribute = attributes.find((attr) => attr.type === "multiSelect");
|
||||
const responseTextAttribute = attributes.find((attr) => attr.type === "text");
|
||||
expect(responseNumberAttribute).toEqual({
|
||||
id: numberAttribute.id,
|
||||
name: numberAttribute.name,
|
||||
optionId: numberAttributeOption.id,
|
||||
option: +numberAttributeOption.value,
|
||||
type: "number",
|
||||
});
|
||||
expect(responseSingleSelectAttribute).toEqual({
|
||||
id: singleSelectAttribute.id,
|
||||
name: singleSelectAttribute.name,
|
||||
optionId: singleSelectAttributeOption.id,
|
||||
option: singleSelectAttributeOption.value,
|
||||
type: "singleSelect",
|
||||
});
|
||||
expect(responseMultiSelectAttribute).toEqual({
|
||||
id: multiSelectAttribute.id,
|
||||
name: multiSelectAttribute.name,
|
||||
options: [
|
||||
{
|
||||
optionId: multiSelectAttributeOption2.id,
|
||||
option: multiSelectAttributeOption2.value,
|
||||
},
|
||||
{
|
||||
optionId: multiSelectAttributeOption.id,
|
||||
option: multiSelectAttributeOption.value,
|
||||
},
|
||||
],
|
||||
type: "multiSelect",
|
||||
});
|
||||
expect(responseTextAttribute).toEqual({
|
||||
id: textAttribute.id,
|
||||
name: textAttribute.name,
|
||||
optionId: textAttributeOption.id,
|
||||
option: textAttributeOption.value,
|
||||
type: "text",
|
||||
});
|
||||
}
|
||||
|
||||
it("should get all the memberships of the org paginated", async () => {
|
||||
return request(app.getHttpServer())
|
||||
.get(`/v2/organizations/${org.id}/memberships?skip=1&take=1`)
|
||||
@@ -182,6 +347,25 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("should get the membership of the org", async () => {
|
||||
return request(app.getHttpServer())
|
||||
.get(`/v2/organizations/${org.id}/memberships/${membership2.id}`)
|
||||
.expect(200)
|
||||
.then((response) => {
|
||||
const responseBody: GetOrgMembership = response.body;
|
||||
expect(responseBody.status).toEqual(SUCCESS_STATUS);
|
||||
expect(responseBody.data.id).toEqual(membership2.id);
|
||||
expect(responseBody.data.userId).toEqual(user2.id);
|
||||
expect(responseBody.data.role).toEqual("MEMBER");
|
||||
expect(responseBody.data.user.bio).toEqual(bio);
|
||||
expect(responseBody.data.user.metadata).toEqual(metadata);
|
||||
expect(responseBody.data.user.email).toEqual(user2.email);
|
||||
expect(responseBody.data.user.username).toEqual(user2.username);
|
||||
expect(responseBody.data.teamId).toEqual(org.id);
|
||||
userHasCorrectAttributes(responseBody.data.attributes);
|
||||
});
|
||||
});
|
||||
|
||||
it("should create the membership of the org", async () => {
|
||||
return request(app.getHttpServer())
|
||||
.post(`/v2/organizations/${org.id}/memberships`)
|
||||
@@ -259,7 +443,8 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
|
||||
let userRepositoryFixture: UserRepositoryFixture;
|
||||
let organizationsRepositoryFixture: OrganizationRepositoryFixture;
|
||||
let membershipsRepositoryFixture: MembershipRepositoryFixture;
|
||||
let membershipRepositoryFixture: MembershipRepositoryFixture;
|
||||
let attributesRepositoryFixture: AttributeRepositoryFixture;
|
||||
|
||||
let org: Team;
|
||||
let membership: Membership;
|
||||
@@ -277,7 +462,8 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
|
||||
userRepositoryFixture = new UserRepositoryFixture(moduleRef);
|
||||
organizationsRepositoryFixture = new OrganizationRepositoryFixture(moduleRef);
|
||||
membershipsRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
|
||||
membershipRepositoryFixture = new MembershipRepositoryFixture(moduleRef);
|
||||
attributesRepositoryFixture = new AttributeRepositoryFixture(moduleRef);
|
||||
|
||||
user = await userRepositoryFixture.create({
|
||||
email: userEmail,
|
||||
@@ -289,7 +475,7 @@ describe("Organizations Memberships Endpoints", () => {
|
||||
isOrganization: true,
|
||||
});
|
||||
|
||||
membership = await membershipsRepositoryFixture.create({
|
||||
membership = await membershipRepositoryFixture.create({
|
||||
role: "MEMBER",
|
||||
user: { connect: { id: user.id } },
|
||||
team: { connect: { id: org.id } },
|
||||
|
||||
+5
-9
@@ -20,7 +20,6 @@ import { GetAllOrgMemberships } from "@/modules/organizations/memberships/output
|
||||
import { GetOrgMembership } from "@/modules/organizations/memberships/outputs/get-membership.output";
|
||||
import { UpdateOrgMembership } from "@/modules/organizations/memberships/outputs/update-membership.output";
|
||||
import { OrganizationsMembershipService } from "@/modules/organizations/memberships/services/organizations-membership.service";
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import {
|
||||
Controller,
|
||||
UseGuards,
|
||||
@@ -36,7 +35,6 @@ import {
|
||||
HttpStatus,
|
||||
} from "@nestjs/common";
|
||||
import { ApiHeader, ApiOperation, ApiTags as DocsTags } from "@nestjs/swagger";
|
||||
import { plainToClass } from "class-transformer";
|
||||
|
||||
import { SUCCESS_STATUS } from "@calcom/platform-constants";
|
||||
import { SkipTakePagination } from "@calcom/platform-types";
|
||||
@@ -70,9 +68,7 @@ export class OrganizationsMembershipsController {
|
||||
);
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: memberships.map((membership) =>
|
||||
plainToClass(TeamMembershipOutput, membership, { strategy: "excludeAll" })
|
||||
),
|
||||
data: memberships,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -88,7 +84,7 @@ export class OrganizationsMembershipsController {
|
||||
const membership = await this.organizationsMembershipService.createOrgMembership(orgId, body);
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: plainToClass(TeamMembershipOutput, membership, { strategy: "excludeAll" }),
|
||||
data: membership,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -105,7 +101,7 @@ export class OrganizationsMembershipsController {
|
||||
const membership = await this.organizationsMembershipService.getOrgMembership(orgId, membershipId);
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: plainToClass(TeamMembershipOutput, membership, { strategy: "excludeAll" }),
|
||||
data: membership,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -122,7 +118,7 @@ export class OrganizationsMembershipsController {
|
||||
const membership = await this.organizationsMembershipService.deleteOrgMembership(orgId, membershipId);
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: plainToClass(TeamMembershipOutput, membership, { strategy: "excludeAll" }),
|
||||
data: membership,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -144,7 +140,7 @@ export class OrganizationsMembershipsController {
|
||||
);
|
||||
return {
|
||||
status: SUCCESS_STATUS,
|
||||
data: plainToClass(TeamMembershipOutput, membership, { strategy: "excludeAll" }),
|
||||
data: membership,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+59
-6
@@ -4,8 +4,31 @@ import { PrismaWriteService } from "@/modules/prisma/prisma-write.service";
|
||||
import { MembershipUserSelect } from "@/modules/teams/memberships/teams-memberships.repository";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
|
||||
import { Prisma } from "@calcom/prisma/client";
|
||||
|
||||
import { UpdateOrgMembershipDto } from "./inputs/update-organization-membership.input";
|
||||
|
||||
export type DbOrgMembership = Awaited<ReturnType<OrganizationsMembershipRepository["findOrgMembership"]>>;
|
||||
|
||||
const attributeToUserSelect = {
|
||||
createdByDSyncId: true,
|
||||
weight: true,
|
||||
attributeOption: {
|
||||
select: {
|
||||
id: true,
|
||||
value: true,
|
||||
slug: true,
|
||||
attribute: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
type: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as const satisfies Prisma.AttributeToUserSelect;
|
||||
|
||||
@Injectable()
|
||||
export class OrganizationsMembershipRepository {
|
||||
constructor(private readonly dbRead: PrismaReadService, private readonly dbWrite: PrismaWriteService) {}
|
||||
@@ -16,7 +39,12 @@ export class OrganizationsMembershipRepository {
|
||||
id: membershipId,
|
||||
teamId: organizationId,
|
||||
},
|
||||
include: { user: { select: MembershipUserSelect } },
|
||||
include: {
|
||||
user: { select: MembershipUserSelect },
|
||||
AttributeToUser: {
|
||||
select: attributeToUserSelect,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -26,7 +54,12 @@ export class OrganizationsMembershipRepository {
|
||||
teamId: organizationId,
|
||||
userId,
|
||||
},
|
||||
include: { user: { select: MembershipUserSelect } },
|
||||
include: {
|
||||
user: { select: MembershipUserSelect },
|
||||
AttributeToUser: {
|
||||
select: attributeToUserSelect,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -36,7 +69,12 @@ export class OrganizationsMembershipRepository {
|
||||
id: membershipId,
|
||||
teamId: organizationId,
|
||||
},
|
||||
include: { user: { select: MembershipUserSelect } },
|
||||
include: {
|
||||
user: { select: MembershipUserSelect },
|
||||
AttributeToUser: {
|
||||
select: attributeToUserSelect,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -45,14 +83,24 @@ export class OrganizationsMembershipRepository {
|
||||
create: { ...data, teamId: organizationId },
|
||||
update: { role: data.role, accepted: data.accepted, disableImpersonation: data.disableImpersonation },
|
||||
where: { userId_teamId: { userId: data.userId, teamId: organizationId } },
|
||||
include: { user: { select: MembershipUserSelect } },
|
||||
include: {
|
||||
user: { select: MembershipUserSelect },
|
||||
AttributeToUser: {
|
||||
select: attributeToUserSelect,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
async updateOrgMembership(organizationId: number, membershipId: number, data: UpdateOrgMembershipDto) {
|
||||
return this.dbWrite.prisma.membership.update({
|
||||
data: { ...data },
|
||||
where: { id: membershipId, teamId: organizationId },
|
||||
include: { user: { select: MembershipUserSelect } },
|
||||
include: {
|
||||
user: { select: MembershipUserSelect },
|
||||
AttributeToUser: {
|
||||
select: attributeToUserSelect,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -61,7 +109,12 @@ export class OrganizationsMembershipRepository {
|
||||
where: {
|
||||
teamId: organizationId,
|
||||
},
|
||||
include: { user: { select: MembershipUserSelect } },
|
||||
include: {
|
||||
user: { select: MembershipUserSelect },
|
||||
AttributeToUser: {
|
||||
select: attributeToUserSelect,
|
||||
},
|
||||
},
|
||||
skip,
|
||||
take,
|
||||
});
|
||||
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import { OrganizationMembershipOutput } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, IsNotEmptyObject, ValidateNested } from "class-validator";
|
||||
@@ -11,10 +11,10 @@ export class CreateOrgMembershipOutput {
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@ApiProperty({
|
||||
type: TeamMembershipOutput,
|
||||
type: OrganizationMembershipOutput,
|
||||
})
|
||||
@IsNotEmptyObject()
|
||||
@ValidateNested()
|
||||
@Type(() => TeamMembershipOutput)
|
||||
data!: TeamMembershipOutput;
|
||||
@Type(() => OrganizationMembershipOutput)
|
||||
data!: OrganizationMembershipOutput;
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import { OrganizationMembershipOutput } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { IsEnum } from "class-validator";
|
||||
|
||||
@@ -9,5 +9,5 @@ export class DeleteOrgMembership {
|
||||
@IsEnum([SUCCESS_STATUS, ERROR_STATUS])
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
data!: TeamMembershipOutput;
|
||||
data!: OrganizationMembershipOutput;
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import { OrganizationMembershipOutput } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, IsNotEmptyObject, ValidateNested, IsArray } from "class-validator";
|
||||
@@ -11,11 +11,11 @@ export class GetAllOrgMemberships {
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@ApiProperty({
|
||||
type: TeamMembershipOutput,
|
||||
type: OrganizationMembershipOutput,
|
||||
})
|
||||
@IsNotEmptyObject()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TeamMembershipOutput)
|
||||
@Type(() => OrganizationMembershipOutput)
|
||||
@IsArray()
|
||||
data!: TeamMembershipOutput[];
|
||||
data!: OrganizationMembershipOutput[];
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import { OrganizationMembershipOutput } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, IsNotEmptyObject, ValidateNested } from "class-validator";
|
||||
@@ -11,10 +11,10 @@ export class GetOrgMembership {
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@ApiProperty({
|
||||
type: TeamMembershipOutput,
|
||||
type: OrganizationMembershipOutput,
|
||||
})
|
||||
@IsNotEmptyObject()
|
||||
@ValidateNested()
|
||||
@Type(() => TeamMembershipOutput)
|
||||
data!: TeamMembershipOutput;
|
||||
@Type(() => OrganizationMembershipOutput)
|
||||
data!: OrganizationMembershipOutput;
|
||||
}
|
||||
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import { ApiExtraModels, ApiProperty, getSchemaPath } from "@nestjs/swagger";
|
||||
import { Expose, Type } from "class-transformer";
|
||||
import { IsArray, IsNumber, IsOptional, IsString, ValidateNested } from "class-validator";
|
||||
|
||||
class BaseAttribute {
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
id!: string;
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
name!: string;
|
||||
}
|
||||
|
||||
export class TextAttribute extends BaseAttribute {
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
type!: "text";
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
option!: string;
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
optionId!: string;
|
||||
}
|
||||
|
||||
export class NumberAttribute extends BaseAttribute {
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
type!: "number";
|
||||
|
||||
@IsNumber()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
option!: number;
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
optionId!: string;
|
||||
}
|
||||
|
||||
export class SingleSelectAttribute extends BaseAttribute {
|
||||
@IsString()
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
type!: "singleSelect";
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
option!: string;
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
optionId!: string;
|
||||
}
|
||||
|
||||
class MultiSelectAttributeOption {
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
optionId!: string;
|
||||
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
option!: string;
|
||||
}
|
||||
|
||||
export class MultiSelectAttribute extends BaseAttribute {
|
||||
@IsString()
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
type!: "multiSelect";
|
||||
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Expose()
|
||||
@ApiProperty()
|
||||
options!: MultiSelectAttributeOption[];
|
||||
}
|
||||
|
||||
export type OrgUserAttribute = TextAttribute | NumberAttribute | SingleSelectAttribute | MultiSelectAttribute;
|
||||
|
||||
@ApiExtraModels(BaseAttribute, TextAttribute, NumberAttribute, SingleSelectAttribute, MultiSelectAttribute)
|
||||
export class OrganizationMembershipOutput extends TeamMembershipOutput {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Expose()
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
oneOf: [
|
||||
{ $ref: getSchemaPath(TextAttribute) },
|
||||
{ $ref: getSchemaPath(NumberAttribute) },
|
||||
{ $ref: getSchemaPath(SingleSelectAttribute) },
|
||||
{ $ref: getSchemaPath(MultiSelectAttribute) },
|
||||
],
|
||||
type: "array",
|
||||
})
|
||||
@Type(() => Object)
|
||||
attributes!: OrgUserAttribute[];
|
||||
}
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
import { TeamMembershipOutput } from "@/modules/teams/memberships/outputs/team-membership.output";
|
||||
import { OrganizationMembershipOutput } from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { ApiProperty } from "@nestjs/swagger";
|
||||
import { Type } from "class-transformer";
|
||||
import { IsEnum, IsNotEmptyObject, ValidateNested } from "class-validator";
|
||||
@@ -11,10 +11,10 @@ export class UpdateOrgMembership {
|
||||
status!: typeof SUCCESS_STATUS | typeof ERROR_STATUS;
|
||||
|
||||
@ApiProperty({
|
||||
type: TeamMembershipOutput,
|
||||
type: OrganizationMembershipOutput,
|
||||
})
|
||||
@IsNotEmptyObject()
|
||||
@ValidateNested()
|
||||
@Type(() => TeamMembershipOutput)
|
||||
data!: TeamMembershipOutput;
|
||||
@Type(() => OrganizationMembershipOutput)
|
||||
data!: OrganizationMembershipOutput;
|
||||
}
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
import { DbOrgMembership } from "@/modules/organizations/memberships/organizations-membership.repository";
|
||||
import {
|
||||
MultiSelectAttribute,
|
||||
NumberAttribute,
|
||||
OrganizationMembershipOutput,
|
||||
OrgUserAttribute,
|
||||
SingleSelectAttribute,
|
||||
TextAttribute,
|
||||
} from "@/modules/organizations/memberships/outputs/organization-membership.output";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { plainToClass } from "class-transformer";
|
||||
|
||||
import { groupMembershipAttributes } from "@calcom/platform-libraries";
|
||||
import type { GroupedAttribute } from "@calcom/platform-libraries";
|
||||
|
||||
@Injectable()
|
||||
export class OrganizationsMembershipOutputService {
|
||||
getOrgMembershipsOutput(memberships: NonNullable<DbOrgMembership>[]) {
|
||||
return memberships.map((membership) => this.getOrgMembershipOutput(membership));
|
||||
}
|
||||
|
||||
getOrgMembershipOutput(organizationMembership: NonNullable<DbOrgMembership>) {
|
||||
const { AttributeToUser, ...orgMembership } = organizationMembership;
|
||||
|
||||
const groupedMembershipAttributes: GroupedAttribute[] = groupMembershipAttributes(AttributeToUser);
|
||||
const attributesOutput = this.getAttributesOutput(groupedMembershipAttributes);
|
||||
|
||||
return plainToClass(OrganizationMembershipOutput, { ...orgMembership, attributes: attributesOutput });
|
||||
}
|
||||
|
||||
private getAttributesOutput(attributes: GroupedAttribute[]): OrgUserAttribute[] {
|
||||
return attributes.map((attribute) => {
|
||||
switch (attribute.type) {
|
||||
case "TEXT":
|
||||
return this.getTextAttributeOutput(attribute);
|
||||
case "NUMBER":
|
||||
return this.getNumberAttributeOutput(attribute);
|
||||
case "SINGLE_SELECT":
|
||||
return this.getSingleSelectAttributeOutput(attribute);
|
||||
case "MULTI_SELECT":
|
||||
return this.getMultiSelectAttributeOutput(attribute);
|
||||
default:
|
||||
return this.getTextAttributeOutput(attribute);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private getTextAttributeOutput(attribute: GroupedAttribute): TextAttribute {
|
||||
return {
|
||||
id: attribute.id,
|
||||
name: attribute.name,
|
||||
optionId: attribute.options[0].id,
|
||||
option: attribute.options[0].value,
|
||||
type: "text",
|
||||
};
|
||||
}
|
||||
|
||||
private getNumberAttributeOutput(attribute: GroupedAttribute): NumberAttribute {
|
||||
return {
|
||||
id: attribute.id,
|
||||
name: attribute.name,
|
||||
optionId: attribute.options[0].id,
|
||||
option: +attribute.options[0].value,
|
||||
type: "number",
|
||||
};
|
||||
}
|
||||
|
||||
private getSingleSelectAttributeOutput(attribute: GroupedAttribute): SingleSelectAttribute {
|
||||
return {
|
||||
id: attribute.id,
|
||||
name: attribute.name,
|
||||
optionId: attribute.options[0].id,
|
||||
option: attribute.options[0].value,
|
||||
type: "singleSelect",
|
||||
};
|
||||
}
|
||||
|
||||
private getMultiSelectAttributeOutput(attribute: GroupedAttribute): MultiSelectAttribute {
|
||||
return {
|
||||
id: attribute.id,
|
||||
name: attribute.name,
|
||||
options: attribute.options.map((option: GroupedAttribute["options"][number]) => ({
|
||||
optionId: option.id,
|
||||
option: option.value,
|
||||
})),
|
||||
type: "multiSelect",
|
||||
};
|
||||
}
|
||||
}
|
||||
+25
-8
@@ -1,19 +1,30 @@
|
||||
import { CreateOrgMembershipDto } from "@/modules/organizations/memberships/inputs/create-organization-membership.input";
|
||||
import { OrganizationsMembershipRepository } from "@/modules/organizations/memberships/organizations-membership.repository";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { Injectable, NotFoundException } from "@nestjs/common";
|
||||
|
||||
import { UpdateOrgMembershipDto } from "../inputs/update-organization-membership.input";
|
||||
import { OrganizationsMembershipOutputService } from "./organizations-membership-output.service";
|
||||
|
||||
@Injectable()
|
||||
export class OrganizationsMembershipService {
|
||||
constructor(private readonly organizationsMembershipRepository: OrganizationsMembershipRepository) {}
|
||||
constructor(
|
||||
private readonly organizationsMembershipRepository: OrganizationsMembershipRepository,
|
||||
private readonly organizationsMembershipOutputService: OrganizationsMembershipOutputService
|
||||
) {}
|
||||
|
||||
async getOrgMembership(organizationId: number, membershipId: number) {
|
||||
const membership = await this.organizationsMembershipRepository.findOrgMembership(
|
||||
organizationId,
|
||||
membershipId
|
||||
);
|
||||
return membership;
|
||||
|
||||
if (!membership) {
|
||||
throw new NotFoundException(
|
||||
`Membership with id ${membershipId} within organization id ${organizationId} not found`
|
||||
);
|
||||
}
|
||||
|
||||
return this.organizationsMembershipOutputService.getOrgMembershipOutput(membership);
|
||||
}
|
||||
|
||||
async getOrgMembershipByUserId(organizationId: number, userId: number) {
|
||||
@@ -21,7 +32,13 @@ export class OrganizationsMembershipService {
|
||||
organizationId,
|
||||
userId
|
||||
);
|
||||
return membership;
|
||||
if (!membership) {
|
||||
throw new NotFoundException(
|
||||
`Membership for user with id ${userId} within organization id ${organizationId} not found`
|
||||
);
|
||||
}
|
||||
|
||||
return this.organizationsMembershipOutputService.getOrgMembershipOutput(membership);
|
||||
}
|
||||
|
||||
async getPaginatedOrgMemberships(organizationId: number, skip = 0, take = 250) {
|
||||
@@ -30,7 +47,7 @@ export class OrganizationsMembershipService {
|
||||
skip,
|
||||
take
|
||||
);
|
||||
return memberships;
|
||||
return this.organizationsMembershipOutputService.getOrgMembershipsOutput(memberships);
|
||||
}
|
||||
|
||||
async deleteOrgMembership(organizationId: number, membershipId: number) {
|
||||
@@ -38,7 +55,7 @@ export class OrganizationsMembershipService {
|
||||
organizationId,
|
||||
membershipId
|
||||
);
|
||||
return membership;
|
||||
return this.organizationsMembershipOutputService.getOrgMembershipOutput(membership);
|
||||
}
|
||||
|
||||
async updateOrgMembership(organizationId: number, membershipId: number, data: UpdateOrgMembershipDto) {
|
||||
@@ -47,11 +64,11 @@ export class OrganizationsMembershipService {
|
||||
membershipId,
|
||||
data
|
||||
);
|
||||
return membership;
|
||||
return this.organizationsMembershipOutputService.getOrgMembershipOutput(membership);
|
||||
}
|
||||
|
||||
async createOrgMembership(organizationId: number, data: CreateOrgMembershipDto) {
|
||||
const membership = await this.organizationsMembershipRepository.createOrgMembership(organizationId, data);
|
||||
return membership;
|
||||
return this.organizationsMembershipOutputService.getOrgMembershipOutput(membership);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import { OrganizationsRepository } from "@/modules/organizations/index/organizat
|
||||
import { OrganizationsService } from "@/modules/organizations/index/organizations.service";
|
||||
import { OrganizationsMembershipsController } from "@/modules/organizations/memberships/organizations-membership.controller";
|
||||
import { OrganizationsMembershipRepository } from "@/modules/organizations/memberships/organizations-membership.repository";
|
||||
import { OrganizationsMembershipOutputService } from "@/modules/organizations/memberships/services/organizations-membership-output.service";
|
||||
import { OrganizationsMembershipService } from "@/modules/organizations/memberships/services/organizations-membership.service";
|
||||
import { OrganizationsOrganizationsModule } from "@/modules/organizations/organizations/organizations-organizations.module";
|
||||
import { OrganizationsSchedulesController } from "@/modules/organizations/schedules/organizations-schedules.controller";
|
||||
@@ -98,6 +99,7 @@ import { Module } from "@nestjs/common";
|
||||
EmailService,
|
||||
OrganizationsMembershipRepository,
|
||||
OrganizationsMembershipService,
|
||||
OrganizationsMembershipOutputService,
|
||||
OrganizationsEventTypesService,
|
||||
InputOrganizationsEventTypesService,
|
||||
OutputOrganizationsEventTypesService,
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import { MembershipsModule } from "@/modules/memberships/memberships.module";
|
||||
import { MembershipsRepository } from "@/modules/memberships/memberships.repository";
|
||||
import { OrganizationsRepository } from "@/modules/organizations/index/organizations.repository";
|
||||
import { OrganizationsMembershipRepository } from "@/modules/organizations/memberships/organizations-membership.repository";
|
||||
import { OrganizationsMembershipOutputService } from "@/modules/organizations/memberships/services/organizations-membership-output.service";
|
||||
import { OrganizationsMembershipService } from "@/modules/organizations/memberships/services/organizations-membership.service";
|
||||
import { ManagedOrganizationsRepository } from "@/modules/organizations/organizations/managed-organizations.repository";
|
||||
import { OrganizationsOrganizationsController } from "@/modules/organizations/organizations/organizations-organizations.controller";
|
||||
@@ -23,6 +24,7 @@ import { Module } from "@nestjs/common";
|
||||
ManagedOrganizationsBillingService,
|
||||
OrganizationsRepository,
|
||||
OrganizationsMembershipService,
|
||||
OrganizationsMembershipOutputService,
|
||||
OrganizationsMembershipRepository,
|
||||
ManagedOrganizationsOutputService,
|
||||
],
|
||||
|
||||
@@ -4,6 +4,7 @@ import { AppsRepository } from "@/modules/apps/apps.repository";
|
||||
import { CredentialsRepository } from "@/modules/credentials/credentials.repository";
|
||||
import { OrganizationsDelegationCredentialRepository } from "@/modules/organizations/delegation-credentials/organizations-delegation-credential.repository";
|
||||
import { OrganizationsMembershipRepository } from "@/modules/organizations/memberships/organizations-membership.repository";
|
||||
import { OrganizationsMembershipOutputService } from "@/modules/organizations/memberships/services/organizations-membership-output.service";
|
||||
import { OrganizationsMembershipService } from "@/modules/organizations/memberships/services/organizations-membership.service";
|
||||
import { PrismaModule } from "@/modules/prisma/prisma.module";
|
||||
import { SelectedCalendarsController } from "@/modules/selected-calendars/controllers/selected-calendars.controller";
|
||||
@@ -22,6 +23,7 @@ import { Module } from "@nestjs/common";
|
||||
CredentialsRepository,
|
||||
AppsRepository,
|
||||
OrganizationsMembershipService,
|
||||
OrganizationsMembershipOutputService,
|
||||
OrganizationsDelegationCredentialRepository,
|
||||
OrganizationsMembershipRepository,
|
||||
SelectedCalendarsService,
|
||||
|
||||
Reference in New Issue
Block a user