Skip to content

Commit

Permalink
nsc-events-nestjs-100-archive-event-functionality (#101)
Browse files Browse the repository at this point in the history
* Added isArchived field to activity schema and DTO's

* Added archive event service method and endpoint

* Added isArchived field to activity.controller.spec.ts

* Added isArchived field to createMockActivity.ts

* Updated request method from POST to PUT
  • Loading branch information
Ali-7s authored Apr 18, 2024
1 parent e21ea33 commit 9cdb1f6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/activity/controllers/activity/activity.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('ActivityController', () => {
eventAccessibility: 'Wheelchair accessible venue.',
eventNote: 'This is a sample note.',
isHidden: false,
isArchived: false,
};

const mockUpdateActivity = {
Expand Down Expand Up @@ -103,6 +104,7 @@ describe('ActivityController', () => {
eventAccessibility: 'Wheelchair accessible venue updated.',
eventNote: 'This is a sample note.',
isHidden: false,
isArchived: false,
};

beforeEach(async () => {
Expand Down Expand Up @@ -211,6 +213,7 @@ describe('ActivityController', () => {
eventAccessibility: 'Wheelchair accessible venue.',
eventNote: 'This is a sample note.',
isHidden: false,
isArchived: false,
};
const expectedResponse = {
activity: mockCreateEvent,
Expand Down Expand Up @@ -260,6 +263,7 @@ describe('ActivityController', () => {
eventAccessibility: 'Wheelchair accessible venue.',
eventNote: 'This is a sample note.',
isHidden: false,
isArchived: false,
};
const errorMessage = 'Error occurred while creating event';
jest
Expand Down
21 changes: 21 additions & 0 deletions src/activity/controllers/activity/activity.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,25 @@ export class ActivityController {
throw new UnauthorizedException();
}
}

@Put('archive/:id')
@UseGuards(AuthGuard())
async archiveActivityById(
@Param('id') id: string,
@Req() req: any,
): Promise<{ archivedActivity: Activity; message: string }> {
const preOperationActivity: Activity =
await this.activityService.getActivityById(id);
if (req.user.role === Role.admin) {
return this.activityService.archiveActivityById(id);
}
if (
preOperationActivity.createdByUser.equals(req.user._id) &&
req.user.role === Role.creator
) {
return await this.activityService.archiveActivityById(id);
} else {
throw new UnauthorizedException();
}
}
}
4 changes: 4 additions & 0 deletions src/activity/dto/create-activity.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,8 @@ export class CreateActivityDto {
@IsOptional()
@IsBoolean()
readonly isHidden: boolean;

@IsOptional()
@IsBoolean()
readonly isArchived: boolean;
}
4 changes: 4 additions & 0 deletions src/activity/dto/update-activity.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,8 @@ export class UpdateActivityDto {
@IsOptional()
@IsBoolean()
readonly isHidden: boolean;

@IsOptional()
@IsBoolean()
readonly isArchived: boolean;
}
3 changes: 3 additions & 0 deletions src/activity/schemas/activity.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ export class Activity {

@Prop({ default: false })
isHidden: boolean;

@Prop({ default: false })
isArchived: boolean;
}

export const ActivitySchema = SchemaFactory.createForClass(Activity);
Expand Down
22 changes: 22 additions & 0 deletions src/activity/services/activity/activity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,26 @@ export class ActivityService {
message: 'Activity deleted successfully.',
};
}

async archiveActivityById(
id: string,
): Promise<{ archivedActivity: Activity; message: string }> {
const isValidId = mongoose.isValidObjectId(id);
// if provided ID is invalid, throw BadRequestException exception
if (!isValidId) {
throw new BadRequestException('Invalid ID. Please enter correct id.');
}
const activity = await this.activityModel.findById(id).exec();
// if no activity found with given ID, throw NotFoundException exception
if (!activity) {
throw new NotFoundException(`Activity with ID ${id} not found.`);
}
const archivedActivity = await this.activityModel
.findByIdAndUpdate(id, { isArchived: true })
.exec();
return {
archivedActivity,
message: 'Activity archived successfully.',
};
}
}
1 change: 1 addition & 0 deletions test/mock-data/createMockActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const createMockActivity: CreateActivityDto = {
eventAccessibility: 'Wheelchair accessible venue.',
eventNote: 'This is a sample note.',
isHidden: false,
isArchived: false,
};

export default createMockActivity;

0 comments on commit 9cdb1f6

Please sign in to comment.