Skip to content

Commit fd65036

Browse files
committed
fix: update group method
1 parent e7e9bc3 commit fd65036

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ValidationSchema } from '@douglasneuroinformatics/libnest/core';
22
import { PartialType } from '@nestjs/swagger';
3-
import { $UpdateGroupData, type GroupSettings } from '@opendatacapture/schemas/group';
3+
import { $UpdateGroupData, type GroupSettings, type GroupType } from '@opendatacapture/schemas/group';
44

55
import { CreateGroupDto } from './create-group.dto';
66

@@ -9,4 +9,5 @@ export class UpdateGroupDto extends PartialType(CreateGroupDto) {
99
accessibleInstrumentIds?: string[];
1010
override name?: string;
1111
settings?: GroupSettings;
12+
override type?: GroupType;
1213
}

apps/api/src/groups/groups.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common';
2+
import type { Prisma } from '@prisma/client';
23

34
import { accessibleQuery } from '@/ability/ability.utils';
45
import type { EntityOperationOptions } from '@/core/types';
@@ -61,6 +62,15 @@ export class GroupsService {
6162
{ accessibleInstrumentIds, ...data }: UpdateGroupDto,
6263
{ ability }: EntityOperationOptions = {}
6364
) {
65+
const where: Prisma.GroupModelWhereInput = { AND: [accessibleQuery(ability, 'update', 'Group')], id };
66+
const group = await this.groupModel.findFirst({ where });
67+
if (!group) {
68+
throw new NotFoundException(`Failed to find group with ID: ${id}`);
69+
}
70+
const exists = typeof data.name === 'string' && (await this.groupModel.exists({ name: group.name }));
71+
if (exists) {
72+
throw new ConflictException(`Group with name '${group.name}' already exists!`);
73+
}
6474
return this.groupModel.update({
6575
data: {
6676
accessibleInstruments: accessibleInstrumentIds

apps/web/src/features/group/components/ManageGroupForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type InstrumentOptions = {
2020

2121
export type ManageGroupFormProps = {
2222
availableInstruments: UnilingualInstrumentInfo[];
23-
onSubmit: (data: UpdateGroupData) => Promisable<any>;
23+
onSubmit: (data: Partial<UpdateGroupData>) => Promisable<any>;
2424
};
2525

2626
export const ManageGroupForm = ({ availableInstruments, onSubmit }: ManageGroupFormProps) => {

packages/schemas/src/group/group.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ export const $CreateGroupData = z.object({
2828
});
2929

3030
export type UpdateGroupData = z.infer<typeof $UpdateGroupData>;
31-
export const $UpdateGroupData = z
32-
.object({
33-
accessibleInstrumentIds: z.array(z.string()),
34-
name: z.string().min(1),
31+
export const $UpdateGroupData = $Group
32+
.omit({
33+
subjectIds: true,
34+
userIds: true
35+
})
36+
.extend({
3537
settings: $GroupSettings.partial()
3638
})
3739
.partial();

0 commit comments

Comments
 (0)