Files
sign/packages/lib/server-only/team/get-teams.ts

54 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-12-05 22:14:47 +09:00
import type { z } from 'zod';
import { prisma } from '@documenso/prisma';
2025-02-17 22:46:36 +11:00
import { SubscriptionSchema } from '@documenso/prisma/generated/zod/modelSchema/SubscriptionSchema';
2025-01-02 15:33:37 +11:00
import { TeamMemberSchema } from '@documenso/prisma/generated/zod/modelSchema/TeamMemberSchema';
import { TeamSchema } from '@documenso/prisma/generated/zod/modelSchema/TeamSchema';
export type GetTeamsOptions = {
userId: number;
};
2024-12-05 22:14:47 +09:00
export const ZGetTeamsResponseSchema = TeamSchema.extend({
currentTeamMember: TeamMemberSchema.pick({
role: true,
}),
2025-02-17 22:46:36 +11:00
subscription: SubscriptionSchema.pick({
status: true,
}).nullable(),
2024-12-05 22:14:47 +09:00
}).array();
export type TGetTeamsResponse = z.infer<typeof ZGetTeamsResponseSchema>;
export const getTeams = async ({ userId }: GetTeamsOptions): Promise<TGetTeamsResponse> => {
const teams = await prisma.team.findMany({
where: {
members: {
some: {
userId,
},
},
},
include: {
2025-02-17 22:46:36 +11:00
subscription: {
select: {
status: true,
},
},
members: {
where: {
userId,
},
select: {
role: true,
},
},
},
});
return teams.map(({ members, ...team }) => ({
...team,
currentTeamMember: members[0],
}));
};