mirror of
https://github.com/documenso/documenso.git
synced 2026-08-26 00:02:43 +10:00
fix(team): merge main and update analytics filters
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { DEFAULT_ANALYTICS_PERIOD, resolveAnalyticsPeriod } from '@documenso/lib/server-only/team/analytics-period';
|
||||
import { getTeamById } from '@documenso/lib/server-only/team/get-team';
|
||||
import { getTeamAnalytics } from '@documenso/lib/server-only/team/get-team-analytics';
|
||||
import { canExecuteTeamAction } from '@documenso/lib/utils/teams';
|
||||
|
||||
import { authenticatedProcedure } from '../trpc';
|
||||
import { ZGetTeamAnalyticsRequestSchema, ZGetTeamAnalyticsResponseSchema } from './get-team-analytics.types';
|
||||
|
||||
export const getTeamAnalyticsRoute = authenticatedProcedure
|
||||
.input(ZGetTeamAnalyticsRequestSchema)
|
||||
.output(ZGetTeamAnalyticsResponseSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { teamId, period, timezone, senderIds } = input;
|
||||
const { user } = ctx;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
teamId,
|
||||
period,
|
||||
senderIds,
|
||||
},
|
||||
});
|
||||
|
||||
const team = await getTeamById({ userId: user.id, teamId });
|
||||
|
||||
// Analytics are restricted to team admins and managers (MANAGE_TEAM).
|
||||
if (!canExecuteTeamAction('MANAGE_TEAM', team.currentTeamRole)) {
|
||||
throw new AppError(AppErrorCode.UNAUTHORIZED, {
|
||||
message: 'You are not allowed to view analytics for this team',
|
||||
});
|
||||
}
|
||||
|
||||
const { start, end } = resolveAnalyticsPeriod({
|
||||
period: period ?? DEFAULT_ANALYTICS_PERIOD,
|
||||
timezone,
|
||||
});
|
||||
|
||||
return await getTeamAnalytics({
|
||||
userId: user.id,
|
||||
teamId,
|
||||
periodStart: start,
|
||||
periodEnd: end,
|
||||
senderIds,
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Calendar period presets for the team analytics dashboard.
|
||||
*
|
||||
* Kept structurally in sync with `ZAnalyticsPeriodSchema` in
|
||||
* `@documenso/lib/server-only/team/get-team-analytics`. This schema is duplicated
|
||||
* here (rather than imported) so the request types stay client-safe and never
|
||||
* pull server-only code into the browser bundle. The compiler enforces parity
|
||||
* where the resolved `period` is handed to `resolveAnalyticsPeriod` in the route.
|
||||
*/
|
||||
export const ZAnalyticsPeriodSchema = z.enum([
|
||||
'week',
|
||||
'month',
|
||||
'quarter',
|
||||
'year',
|
||||
'lastMonth',
|
||||
'last7Days',
|
||||
'last30Days',
|
||||
]);
|
||||
|
||||
export const ZGetTeamAnalyticsRequestSchema = z.object({
|
||||
teamId: z.number(),
|
||||
period: ZAnalyticsPeriodSchema.optional(),
|
||||
timezone: z.string().optional(),
|
||||
senderIds: z.array(z.number()).optional(),
|
||||
});
|
||||
|
||||
export const ZGetTeamAnalyticsResponseSchema = z.object({
|
||||
sent: z.number(),
|
||||
draft: z.number(),
|
||||
pending: z.number(),
|
||||
completed: z.number(),
|
||||
declined: z.number(),
|
||||
});
|
||||
|
||||
export type TGetTeamAnalyticsRequest = z.infer<typeof ZGetTeamAnalyticsRequestSchema>;
|
||||
export type TGetTeamAnalyticsResponse = z.infer<typeof ZGetTeamAnalyticsResponseSchema>;
|
||||
@@ -16,6 +16,7 @@ import { findTeamGroupsRoute } from './find-team-groups';
|
||||
import { findTeamMembersRoute } from './find-team-members';
|
||||
import { findTeamsRoute } from './find-teams';
|
||||
import { getTeamRoute } from './get-team';
|
||||
import { getTeamAnalyticsRoute } from './get-team-analytics';
|
||||
import { getTeamMembersRoute } from './get-team-members';
|
||||
import {
|
||||
ZCreateTeamEmailVerificationMutationSchema,
|
||||
@@ -33,6 +34,7 @@ import { updateTeamSettingsRoute } from './update-team-settings';
|
||||
export const teamRouter = router({
|
||||
find: findTeamsRoute,
|
||||
get: getTeamRoute,
|
||||
getAnalytics: getTeamAnalyticsRoute,
|
||||
create: createTeamRoute,
|
||||
update: updateTeamRoute,
|
||||
delete: deleteTeamRoute,
|
||||
|
||||
Reference in New Issue
Block a user