Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ADMIN role #1503

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nanoid } from 'nanoid';
import { GoalHistory, Comment, Activity, User, Goal } from '@prisma/client';
import { GoalHistory, Comment, Activity, User, Goal, Role } from '@prisma/client';
import { TRPCError } from '@trpc/server';

import { GoalCommon, GoalUpdate, dependencyKind } from '../schema/goal';
Expand Down Expand Up @@ -50,7 +50,7 @@
* @param input goal FormData
* @returns new goal id
*/
export const createGoal = async (activityId: string, input: GoalCommon) => {
export const createGoal = async (input: GoalCommon, activityId: string, role: Role) => {
const id = nanoid();

await prisma.$executeRaw`
Expand Down Expand Up @@ -106,7 +106,7 @@

return {
...goal,
...addCalclulatedGoalsFields(goal, activityId),
...addCalclulatedGoalsFields(goal, activityId, role),
};
};

Expand Down Expand Up @@ -336,7 +336,7 @@
`;
}
});
} catch (error: any) {

Check warning on line 339 in src/utils/db.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', cause: error, message: error?.message });
}
};
6 changes: 3 additions & 3 deletions trpc/queries/goals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Estimate, EstimateToGoal, Goal, GoalAchieveCriteria, Prisma, State, StateType } from '@prisma/client';
import { Estimate, EstimateToGoal, Goal, GoalAchieveCriteria, Prisma, Role, State, StateType } from '@prisma/client';

import { QueryWithFilters } from '../../src/schema/common';

Expand Down Expand Up @@ -114,7 +114,7 @@
}
: {};

let orderBy: any = [];

Check warning on line 117 in trpc/queries/goals.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

if (data.sort?.updatedAt) {
orderBy = [{ updatedAt: data.sort.updatedAt }];
Expand Down Expand Up @@ -482,7 +482,7 @@
);
};

export const addCalclulatedGoalsFields = (goal: any, activityId: string) => {
export const addCalclulatedGoalsFields = (goal: any, activityId: string, role: Role) => {
const _isOwner = goal.ownerId === activityId;
const _isParticipant = goal.participants?.some((participant: any) => participant?.id === activityId);
const _isWatching = goal.watchers?.some((watcher: any) => watcher?.id === activityId);
Expand Down Expand Up @@ -510,7 +510,7 @@
}
checkParent(goal.project);

const _isEditable = _isOwner || _isIssuer || parentOwner;
const _isEditable = _isOwner || _isIssuer || parentOwner || role === 'ADMIN';
9teen90nine marked this conversation as resolved.
Show resolved Hide resolved

return {
_isOwner,
Expand Down
53 changes: 32 additions & 21 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ export const goal = router({
}),
)
.query(async ({ ctx, input: { query, limit, skip, cursor } }) => {
const { activityId, role } = ctx.session.user;

const [items, count] = await Promise.all([
prisma.goal.findMany({
take: limit + 1,
skip,
cursor: cursor ? { id: cursor } : undefined,
...(query ? goalsFilter(query, ctx.session.user.activityId) : {}),
...(query ? goalsFilter(query, activityId) : {}),
orderBy: {
id: 'asc',
},
Expand All @@ -136,9 +138,9 @@ export const goal = router({
return {
items: items.map((g) => ({
...g,
...addCalclulatedGoalsFields(g, ctx.session.user.activityId),
...addCalclulatedGoalsFields(g, activityId, role),
_estimate: getEstimateListFormJoin(g),
_project: g.project ? addCalculatedProjectFields(g.project, ctx.session.user.activityId) : null,
_project: g.project ? addCalculatedProjectFields(g.project, activityId, role) : null,
})),
nextCursor,
meta: {
Expand All @@ -157,6 +159,7 @@ export const goal = router({
getById: protectedProcedure.input(z.string()).query(async ({ ctx, input }) => {
// try to recognize shot id like: FRNTND-23
const [projectId, scopeIdStr] = input.split('-');
const { activityId, role } = ctx.session.user;

if (!projectId) return null;

Expand Down Expand Up @@ -225,8 +228,8 @@ export const goal = router({

return {
...goal,
...addCalclulatedGoalsFields(goal, ctx.session.user.activityId),
_project: goal.project ? addCalculatedProjectFields(goal.project, ctx.session.user.activityId) : null,
...addCalclulatedGoalsFields(goal, activityId, role),
_project: goal.project ? addCalculatedProjectFields(goal.project, activityId, role) : null,
_estimate: getEstimateListFormJoin(goal),
_activityFeed: mixHistoryWithComments(history, goal.comments),
_relations: makeGoalRelationMap({
Expand All @@ -243,7 +246,7 @@ export const goal = router({
if (!input.owner.id) return null;
if (!input.parent.id) return null;

const { activityId } = ctx.session.user;
const { activityId, role } = ctx.session.user;

const actualProject = await prisma.project.findUnique({
where: { id: input.parent.id },
Expand All @@ -259,7 +262,7 @@ export const goal = router({
}

try {
const newGoal = await createGoal(activityId, input);
const newGoal = await createGoal(input, activityId, role);

const recipients = Array.from(
new Set(
Expand Down Expand Up @@ -299,7 +302,7 @@ export const goal = router({

if (!actualGoal) return null;

const { activityId } = ctx.session.user;
const { activityId, role } = ctx.session.user;

try {
await changeGoalProject(input.id, input.projectId);
Expand Down Expand Up @@ -327,13 +330,15 @@ export const goal = router({

return {
...goal,
...addCalclulatedGoalsFields(goal, activityId),
...addCalclulatedGoalsFields(goal, activityId, role),
};
} catch (error: any) {
throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: String(error.message), cause: error });
}
}),
update: protectedProcedure.input(goalUpdateSchema).mutation(async ({ ctx, input }) => {
const { activityId, role } = ctx.session.user;

const actualGoal = await prisma.goal.findUnique({
where: { id: input.id },
include: {
Expand All @@ -352,7 +357,7 @@ export const goal = router({

if (!actualGoal) return null;

const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);
const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, activityId, role);

if (!_isEditable) {
return null;
Expand Down Expand Up @@ -423,7 +428,7 @@ export const goal = router({
});
}

const correctEstimate = await findOrCreateEstimate(input.estimate, ctx.session.user.activityId, actualGoal.id);
const correctEstimate = await findOrCreateEstimate(input.estimate, activityId, actualGoal.id);
const previousEstimate = actualGoal.estimate.length
? actualGoal.estimate[actualGoal.estimate.length - 1]
: null;
Expand Down Expand Up @@ -473,7 +478,7 @@ export const goal = router({
},
history: {
createMany: {
data: history.map((record) => ({ ...record, activityId: ctx.session.user.activityId })),
data: history.map((record) => ({ ...record, activityId })),
},
},
goalAsCriteria: actualGoal.goalAsCriteria
Expand Down Expand Up @@ -530,8 +535,8 @@ export const goal = router({

return {
...goal,
...addCalclulatedGoalsFields(goal, ctx.session.user.activityId),
_project: goal.project ? addCalculatedProjectFields(goal.project, ctx.session.user.activityId) : null,
...addCalclulatedGoalsFields(goal, activityId, role),
_project: goal.project ? addCalculatedProjectFields(goal.project, activityId, role) : null,
_estimate: getEstimateListFormJoin(goal),
_activityFeed: [],
};
Expand Down Expand Up @@ -572,6 +577,8 @@ export const goal = router({
toggleArchive: protectedProcedure
.input(toggleGoalArchiveSchema)
.mutation(async ({ input: { id, archived }, ctx }) => {
const { activityId, role } = ctx.session.user;

const actualGoal = await prisma.goal.findFirst({
where: {
id,
Expand All @@ -590,7 +597,7 @@ export const goal = router({
return null;
}

const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);
const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, activityId, role);

if (!_isEditable) {
return null;
Expand All @@ -605,7 +612,7 @@ export const goal = router({
create: {
subject: 'state',
action: 'archive',
activityId: ctx.session.user.activityId,
activityId,
nextValue: archived ? 'move to archive' : 'move from archive',
},
},
Expand Down Expand Up @@ -638,6 +645,8 @@ export const goal = router({
}
}),
switchState: protectedProcedure.input(goalStateChangeSchema).mutation(async ({ input, ctx }) => {
const { activityId, role } = ctx.session.user;

const actualGoal = await prisma.goal.findFirst({
where: {
id: input.id,
Expand All @@ -657,7 +666,7 @@ export const goal = router({
return null;
}

const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);
const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, activityId, role);

if (!_isEditable) {
return null;
Expand All @@ -677,7 +686,7 @@ export const goal = router({
action: 'change',
previousValue: actualGoal.stateId,
nextValue: input.state.id,
activityId: ctx.session.user.activityId,
activityId,
},
},
goalAsCriteria: actualGoal.goalAsCriteria
Expand Down Expand Up @@ -708,7 +717,7 @@ export const goal = router({
subject: 'criteria',
action: nowIsCompleted ? 'complete' : 'uncomplete',
nextValue: actualGoal.goalAsCriteria.id,
activityId: ctx.session.user.activityId,
activityId,
},
}),
);
Expand Down Expand Up @@ -748,6 +757,8 @@ export const goal = router({
createComment: protectedProcedure.input(goalCommentCreateSchema).mutation(async ({ ctx, input }) => {
if (!input.goalId) return null;

const { activityId, role } = ctx.session.user;

const [commentAuthor, actualGoal, pushState] = await Promise.all([
prisma.activity.findUnique({
where: { id: ctx.session.user.activityId },
Expand All @@ -771,7 +782,7 @@ export const goal = router({
if (!commentAuthor) return null;
if (!actualGoal) return null;

const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, ctx.session.user.activityId);
const { _isEditable, _shortId } = addCalclulatedGoalsFields(actualGoal, activityId, role);

try {
// We want to see state changes record and comment next in activity feed.
Expand All @@ -797,7 +808,7 @@ export const goal = router({
action: 'change',
previousValue: actualGoal.stateId,
nextValue: input.stateId,
activityId: ctx.session.user.activityId,
activityId,
},
}
: undefined,
Expand Down
Loading
Loading