2022-04-02 01:46:24 +00:00
|
|
|
import { z } from "zod";
|
2022-03-26 23:58:22 +00:00
|
|
|
|
2023-05-05 14:33:24 +00:00
|
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
2022-10-21 19:54:28 +00:00
|
|
|
import { _MembershipModel as Membership, _TeamModel } from "@calcom/prisma/zod";
|
2022-11-07 14:48:32 +00:00
|
|
|
import { stringOrNumber } from "@calcom/prisma/zod-utils";
|
2022-10-21 19:54:28 +00:00
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaQueryIdAsString } from "~/lib/validations/shared/queryIdString";
|
|
|
|
import { schemaQueryIdParseInt } from "~/lib/validations/shared/queryIdTransformParseInt";
|
2022-03-30 15:37:51 +00:00
|
|
|
|
2022-04-02 01:46:24 +00:00
|
|
|
export const schemaMembershipBaseBodyParams = Membership.omit({});
|
2022-10-21 19:54:28 +00:00
|
|
|
|
2022-04-02 01:46:24 +00:00
|
|
|
const schemaMembershipRequiredParams = z.object({
|
|
|
|
teamId: z.number(),
|
|
|
|
});
|
|
|
|
|
2023-08-28 06:57:01 +00:00
|
|
|
export const membershipCreateBodySchema = Membership.omit({ id: true })
|
|
|
|
.partial({
|
|
|
|
accepted: true,
|
|
|
|
role: true,
|
|
|
|
disableImpersonation: true,
|
|
|
|
})
|
|
|
|
.transform((v) => ({
|
|
|
|
accepted: false,
|
|
|
|
role: MembershipRole.MEMBER,
|
|
|
|
disableImpersonation: false,
|
|
|
|
...v,
|
|
|
|
}));
|
2022-10-21 19:54:28 +00:00
|
|
|
|
|
|
|
export const membershipEditBodySchema = Membership.omit({
|
|
|
|
/** To avoid complication, let's avoid updating these, instead you can delete and create a new invite */
|
|
|
|
teamId: true,
|
|
|
|
userId: true,
|
|
|
|
})
|
|
|
|
.partial({
|
|
|
|
accepted: true,
|
|
|
|
role: true,
|
|
|
|
disableImpersonation: true,
|
|
|
|
})
|
|
|
|
.strict();
|
|
|
|
|
2022-04-02 01:46:24 +00:00
|
|
|
export const schemaMembershipBodyParams = schemaMembershipBaseBodyParams.merge(
|
|
|
|
schemaMembershipRequiredParams
|
|
|
|
);
|
2022-03-30 15:37:51 +00:00
|
|
|
|
2022-10-21 19:54:28 +00:00
|
|
|
export const schemaMembershipPublic = Membership.merge(z.object({ team: _TeamModel }).partial());
|
|
|
|
|
|
|
|
/** We extract userId and teamId from compound ID string */
|
|
|
|
export const membershipIdSchema = schemaQueryIdAsString
|
|
|
|
// So we can query additional team data in memberships
|
|
|
|
.merge(z.object({ teamId: z.union([stringOrNumber, z.array(stringOrNumber)]) }).partial())
|
|
|
|
.transform((v, ctx) => {
|
|
|
|
const [userIdStr, teamIdStr] = v.id.split("_");
|
|
|
|
const userIdInt = schemaQueryIdParseInt.safeParse({ id: userIdStr });
|
|
|
|
const teamIdInt = schemaQueryIdParseInt.safeParse({ id: teamIdStr });
|
|
|
|
if (!userIdInt.success) {
|
|
|
|
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "userId is not a number" });
|
|
|
|
return z.NEVER;
|
|
|
|
}
|
|
|
|
if (!teamIdInt.success) {
|
|
|
|
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "teamId is not a number " });
|
|
|
|
return z.NEVER;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
userId: userIdInt.data.id,
|
|
|
|
teamId: teamIdInt.data.id,
|
|
|
|
};
|
|
|
|
});
|