Files
esign/packages/trpc/server/organisation-router/find-organisation-email-domain.ts
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

98 lines
2.3 KiB
TypeScript

import type { EmailDomainStatus } 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 {
ZFindOrganisationEmailDomainsRequestSchema,
ZFindOrganisationEmailDomainsResponseSchema,
} from './find-organisation-email-domain.types';
type FindOrganisationEmailDomainsOptions = {
userId: number;
organisationId: string;
emailDomainId?: string;
statuses?: EmailDomainStatus[];
query?: string;
page?: number;
perPage?: number;
};
export const findOrganisationEmailDomains = async ({
userId,
organisationId,
emailDomainId,
statuses = [],
query,
page = 1,
perPage = 100,
}: FindOrganisationEmailDomainsOptions) => {
const organisation = await prisma.organisation.findFirst({
where: buildOrganisationWhereQuery({ organisationId, userId }),
});
if (!organisation) {
throw new AppError(AppErrorCode.NOT_FOUND);
}
const whereClause: Prisma.EmailDomainWhereInput = {
organisationId: organisation.id,
status: statuses.length > 0 ? { in: statuses } : undefined,
};
if (emailDomainId) {
whereClause.id = emailDomainId;
}
if (query) {
whereClause.domain = {
contains: query,
};
}
const [data, count] = await Promise.all([
prisma.emailDomain.findMany({
where: whereClause,
skip: Math.max(page - 1, 0) * perPage,
take: perPage,
orderBy: {
createdAt: 'desc',
},
select: {
id: true,
status: true,
organisationId: true,
domain: true,
selector: true,
createdAt: true,
updatedAt: true,
lastVerifiedAt: true,
_count: {
select: {
emails: true,
},
},
},
}),
prisma.emailDomain.count({
where: whereClause,
}),
]);
const mappedData = data.map((item) => ({
...item,
emailCount: item._count.emails,
}));
return {
data: mappedData,
count,
currentPage: page,
perPage,
totalPages: Math.ceil(count / perPage),
} satisfies FindResultResponse<typeof mappedData>;
};