mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
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.
108 lines
2.1 KiB
TypeScript
108 lines
2.1 KiB
TypeScript
import { EnvelopeType } from '@prisma/client';
|
|
|
|
import { buildTeamWhereQuery } from '@hanzo/esign-lib/utils/teams';
|
|
import { prisma } from '@hanzo/esign-prisma';
|
|
|
|
export type GetRecipientSuggestionsOptions = {
|
|
userId: number;
|
|
teamId: number;
|
|
query: string;
|
|
};
|
|
|
|
export const getRecipientSuggestions = async ({
|
|
userId,
|
|
teamId,
|
|
query,
|
|
}: GetRecipientSuggestionsOptions) => {
|
|
const trimmedQuery = query.trim();
|
|
|
|
const nameEmailFilter = trimmedQuery
|
|
? {
|
|
OR: [
|
|
{
|
|
name: {
|
|
contains: trimmedQuery,
|
|
},
|
|
},
|
|
{
|
|
email: {
|
|
contains: trimmedQuery,
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {};
|
|
|
|
const recipients = await prisma.recipient.findMany({
|
|
where: {
|
|
envelope: {
|
|
type: EnvelopeType.DOCUMENT,
|
|
team: buildTeamWhereQuery({ teamId, userId }),
|
|
},
|
|
...nameEmailFilter,
|
|
},
|
|
select: {
|
|
name: true,
|
|
email: true,
|
|
envelope: {
|
|
select: {
|
|
createdAt: true,
|
|
},
|
|
},
|
|
},
|
|
distinct: ['email'],
|
|
orderBy: {
|
|
envelope: {
|
|
createdAt: 'desc',
|
|
},
|
|
},
|
|
take: 5,
|
|
});
|
|
|
|
if (teamId) {
|
|
const teamMembers = await prisma.organisationMember.findMany({
|
|
where: {
|
|
user: {
|
|
...nameEmailFilter,
|
|
NOT: { id: userId },
|
|
},
|
|
organisationGroupMembers: {
|
|
some: {
|
|
group: {
|
|
teamGroups: {
|
|
some: { teamId },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
take: 5,
|
|
});
|
|
|
|
const uniqueTeamMember = teamMembers.find(
|
|
(member) => !recipients.some((r) => r.email === member.user.email),
|
|
);
|
|
|
|
if (uniqueTeamMember) {
|
|
const teamMemberSuggestion = {
|
|
email: uniqueTeamMember.user.email,
|
|
name: uniqueTeamMember.user.name,
|
|
};
|
|
|
|
const allSuggestions = [...recipients.slice(0, 4), teamMemberSuggestion];
|
|
|
|
return allSuggestions;
|
|
}
|
|
}
|
|
|
|
return recipients;
|
|
};
|