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

nsc-events-nestjs-100-archive-event-functionality #101

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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();
}
}

@Post('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;
Loading