This commit is contained in:
David Nguyen
2025-05-07 15:03:20 +10:00
parent 419bc02171
commit 7abfc9e271
390 changed files with 21254 additions and 12607 deletions

View File

@ -1,7 +1,16 @@
import type { OrganisationGlobalSettings, Prisma, TeamGlobalSettings } from '@prisma/client';
import type { TeamGroup } from '@documenso/prisma/generated/types';
import type { TeamMemberRole } from '@documenso/prisma/generated/types';
import { NEXT_PUBLIC_WEBAPP_URL } from '../constants/app';
import { DocumentSignatureType } from '../constants/document';
import type { TEAM_MEMBER_ROLE_MAP } from '../constants/teams';
import { TEAM_MEMBER_ROLE_HIERARCHY, TEAM_MEMBER_ROLE_PERMISSIONS_MAP } from '../constants/teams';
import {
LOWEST_TEAM_ROLE,
TEAM_MEMBER_ROLE_HIERARCHY,
TEAM_MEMBER_ROLE_PERMISSIONS_MAP,
} from '../constants/teams';
export const formatTeamUrl = (teamUrl: string, baseUrl?: string) => {
const formattedBaseUrl = (baseUrl ?? NEXT_PUBLIC_WEBAPP_URL()).replace(/https?:\/\//, '');
@ -46,11 +55,26 @@ export const isTeamRoleWithinUserHierarchy = (
return TEAM_MEMBER_ROLE_HIERARCHY[currentUserRole].some((i) => i === roleToCheck);
};
export const getHighestTeamRoleInGroup = (groups: TeamGroup[]): TeamMemberRole => {
let highestTeamRole: TeamMemberRole = LOWEST_TEAM_ROLE;
groups.forEach((group) => {
const currentRolePriority = TEAM_MEMBER_ROLE_HIERARCHY[group.teamRole].length;
const highestTeamRolePriority = TEAM_MEMBER_ROLE_HIERARCHY[highestTeamRole].length;
if (currentRolePriority > highestTeamRolePriority) {
highestTeamRole = group.teamRole;
}
});
return highestTeamRole;
};
export const extractTeamSignatureSettings = (
settings?: {
typedSignatureEnabled: boolean;
drawSignatureEnabled: boolean;
uploadSignatureEnabled: boolean;
typedSignatureEnabled: boolean | null;
drawSignatureEnabled: boolean | null;
uploadSignatureEnabled: boolean | null;
} | null,
) => {
if (!settings) {
@ -73,3 +97,99 @@ export const extractTeamSignatureSettings = (
return signatureTypes;
};
// Todo: orgs test
export const buildTeamWhereQuery = (
teamId: number | undefined, // Todo: test if this is okay
userId: number,
roles?: TeamMemberRole[],
): Prisma.TeamWhereUniqueInput => {
// Note: Not using inline ternary since typesafety breaks for some reason.
if (!roles) {
return {
id: teamId,
teamGroups: {
some: {
organisationGroup: {
organisation: {
members: {
some: {
userId,
},
},
},
},
},
},
};
}
return {
id: teamId,
teamGroups: {
some: {
organisationGroup: {
organisation: {
members: {
some: {
userId,
},
},
},
},
teamRole: {
in: roles,
},
},
},
};
};
/**
* Majority of these are null which lets us inherit from the organisation settings.
*/
export const generateDefaultTeamSettings = (): Omit<TeamGlobalSettings, 'id' | 'team'> => {
return {
documentVisibility: null,
documentLanguage: null,
includeSenderDetails: null,
includeSigningCertificate: null,
typedSignatureEnabled: null,
uploadSignatureEnabled: null,
drawSignatureEnabled: null,
brandingEnabled: null,
brandingLogo: null,
brandingUrl: null,
brandingCompanyDetails: null,
brandingHidePoweredBy: null,
};
};
/**
* Derive the final settings for a team.
*
* @param organisationSettings The organisation settings to inherit values from
* @param teamSettings The team settings which can override the organisation settings
*/
export const extractDerivedTeamSettings = (
organisationSettings: Omit<OrganisationGlobalSettings, 'id'>,
teamSettings: Omit<TeamGlobalSettings, 'id'>,
): Omit<OrganisationGlobalSettings, 'id'> => {
const derivedSettings: Omit<OrganisationGlobalSettings, 'id'> = {
...organisationSettings,
};
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
for (const key of Object.keys(derivedSettings) as (keyof typeof derivedSettings)[]) {
const teamValue = teamSettings[key];
if (teamValue !== null) {
// @ts-expect-error Should work
derivedSettings[key] = teamValue;
}
}
return derivedSettings;
};