Files
zooqueen fb4e3df0e3 rename: @hanzo/sign-* → @hanzo/esign-* (full eSign brand consistency)
The product is branded Hanzo eSign (esign.hanzo.ai, repo hanzoai/esign).
Rename all workspace packages @hanzo/sign-* → @hanzo/esign-* (1052 files)
and align docker/Dockerfile turbo --scope/--filter to the actual remix
package name @hanzo/esign (was a pre-existing mismatch with -remix that
broke the build). Protected deps (@hanzo/insights, @hanzo/logo) untouched.
2026-06-21 19:56:09 -07:00

142 lines
3.5 KiB
TypeScript

import type { OrganisationGroupType, OrganisationMemberRole } from '@prisma/client';
import { Prisma } from '@prisma/client';
import { AppError, AppErrorCode } from '@hanzo/esign-lib/errors/app-error';
import type { FindResultResponse } from '@hanzo/esign-lib/types/search-params';
import { buildOrganisationWhereQuery } from '@hanzo/esign-lib/utils/organisations';
import { prisma } from '@hanzo/esign-prisma';
import {
ZFindOrganisationGroupsRequestSchema,
ZFindOrganisationGroupsResponseSchema,
} from './find-organisation-groups.types';
type FindOrganisationGroupsOptions = {
userId: number;
organisationId: string;
organisationGroupId?: string;
organisationRoles?: OrganisationMemberRole[];
types?: OrganisationGroupType[];
query?: string;
page?: number;
perPage?: number;
};
export const findOrganisationGroups = async ({
userId,
organisationId,
organisationGroupId,
organisationRoles = [],
types = [],
query,
page = 1,
perPage = 10,
}: FindOrganisationGroupsOptions) => {
const organisation = await prisma.organisation.findFirst({
where: buildOrganisationWhereQuery({ organisationId, userId }),
});
if (!organisation) {
throw new AppError(AppErrorCode.NOT_FOUND);
}
const whereClause: Prisma.OrganisationGroupWhereInput = {
organisationId: organisation.id,
type:
types.length > 0
? {
in: types,
}
: undefined,
organisationRole:
organisationRoles.length > 0
? {
in: organisationRoles,
}
: undefined,
id: organisationGroupId,
};
if (query) {
whereClause.name = {
contains: query,
};
}
const [data, count] = await Promise.all([
prisma.organisationGroup.findMany({
where: whereClause,
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
orderBy: {
name: 'desc',
},
select: {
id: true,
name: true,
type: true,
organisationId: true,
organisationRole: true,
teamGroups: {
select: {
id: true,
teamId: true,
teamRole: true,
team: {
select: {
id: true,
name: true,
},
},
},
},
organisationGroupMembers: {
select: {
organisationMember: {
select: {
id: true,
user: {
select: {
id: true,
email: true,
name: true,
avatarImageId: true,
},
},
},
},
},
},
},
}),
prisma.organisationGroup.count({
where: whereClause,
}),
]);
const mappedData = data.map((group) => ({
...group,
teams: group.teamGroups.map((teamGroup) => ({
id: teamGroup.team.id,
name: teamGroup.team.name,
teamGroupId: teamGroup.id,
teamRole: teamGroup.teamRole,
})),
members: group.organisationGroupMembers.map(({ organisationMember }) => ({
id: organisationMember.id,
userId: organisationMember.user.id,
name: organisationMember.user.name || '',
email: organisationMember.user.email,
avatarImageId: organisationMember.user.avatarImageId,
})),
}));
return {
data: mappedData,
count,
currentPage: Math.max(page, 1),
perPage,
totalPages: Math.ceil(count / perPage),
} satisfies FindResultResponse<typeof mappedData>;
};