Files
calendar/packages/app-store/routing-forms/lib/getAttributes.ts
T
654a204878 feat: Attribute based routing from Team Routing Form to Team Event(with assigned users matching the attribute logic) (#16823)
* Initial commit

* routingForm to Booking a particular team member working

* Happy path working

* Fixes

* Fix router query params forwarding

* Add basicConfig within app

* Tests

* More tests

* Update packages/app-store/routing-forms/components/SingleForm.tsx

Co-authored-by: Omar López <zomars@me.com>

---------

Co-authored-by: Omar López <zomars@me.com>
Co-authored-by: Benny Joo <sldisek783@gmail.com>
2024-10-08 21:12:32 +05:30

139 lines
3.6 KiB
TypeScript

// TODO: Queries in this file are not optimized. Need to optimize them.
import logger from "@calcom/lib/logger";
import { safeStringify } from "@calcom/lib/safeStringify";
import prisma from "@calcom/prisma";
import type { Attribute } from "../types/types";
async function getAttributeToUserWithMembershipAndAttributesForTeam({ teamId }: { teamId: number }) {
let log = logger.getSubLogger({ prefix: ["getAttributeToUserWithMembershipAndAttributes"] });
const whereClauseForAttributesAssignedToMembersOfTeam = {
member: {
user: {
teams: {
some: {
teamId,
},
},
},
},
};
log.debug(
safeStringify({
teamId,
whereClauseForAttributesAssignedToMembersOfTeam,
})
);
const attributesToUser = await prisma.attributeToUser.findMany({
where: whereClauseForAttributesAssignedToMembersOfTeam,
select: {
member: {
select: {
userId: true,
},
},
attributeOption: {
select: {
id: true,
value: true,
slug: true,
attribute: {
select: { id: true, name: true, type: true, slug: true },
},
},
},
},
});
log.debug("Returned attributesToUser", safeStringify({ attributesToUser }));
return attributesToUser;
}
async function getAttributesAssignedToMembersOfTeam({ teamId }: { teamId: number }) {
let log = logger.getSubLogger({ prefix: ["getAttributeToUserWithMembershipAndAttributes"] });
const whereClauseForAttributesAssignedToMembersOfTeam = {
options: {
some: {
assignedUsers: {
some: {
member: {
user: {
teams: {
some: {
teamId,
},
},
},
},
},
},
},
},
}
log.debug(
safeStringify({
teamId,
whereClauseForAttributesAssignedToMembersOfTeam,
})
);
const attributesToUser = await prisma.attribute.findMany({
where: whereClauseForAttributesAssignedToMembersOfTeam,
select: {
id: true,
name: true,
type: true,
options: {
select: {
id: true,
value: true,
slug: true,
},
},
slug: true,
},
});
return attributesToUser;
}
export async function getAttributesForTeam({ teamId }: { teamId: number }) {
const attributes = await getAttributesAssignedToMembersOfTeam({ teamId });
return attributes satisfies Attribute[];
}
type AttributeId = string;
type AttributeOptionValue = string | string[];
export async function getTeamMembersWithAttributeOptionValuePerAttribute({ teamId }: { teamId: number }) {
const attributesToUser = await getAttributeToUserWithMembershipAndAttributesForTeam({ teamId });
const teamMembers = attributesToUser.reduce((acc, attributeToUser) => {
const { userId } = attributeToUser.member;
const { attribute, value } = attributeToUser.attributeOption;
if (!acc[userId]) {
acc[userId] = { userId, attributes: {} };
}
const attributes = acc[userId].attributes;
const attributeValue = attributes[attribute.id];
if (attributeValue instanceof Array) {
// Push to existing array
attributeValue.push(value);
} else if (attributeValue) {
// Make it an array
attributes[attribute.id] = [attributeValue, value];
} else {
// Set it as a string
attributes[attribute.id] = value;
}
return acc;
}, {} as Record<number, { userId: number; attributes: Record<AttributeId, AttributeOptionValue> }>);
return Object.values(teamMembers);
}