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-8-127-archiveActivityById-conditional-toggle #128

Merged
Merged
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
35 changes: 24 additions & 11 deletions src/activity/services/activity/activity.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export class ActivityService {
const filter: any = {
...tag,
isArchived: query.isArchived || false,
isHidden: query.isHidden || false
isHidden: query.isHidden || false,
};
return await this.activityModel
.find({...filter})
.find({ ...filter })
.sort({ eventDate: 1, _id: 1 })
.limit(resPerPage)
.skip(skip)
Expand All @@ -62,7 +62,10 @@ export class ActivityService {
return activity;
}

async getActivitiesByUserId(query: Query, userId: string): Promise<Activity[]> {
async getActivitiesByUserId(
query: Query,
userId: string,
): Promise<Activity[]> {
const isValidId = mongoose.isValidObjectId(userId);
if (!isValidId) {
throw new BadRequestException('Please enter correct user id.');
Expand All @@ -74,11 +77,11 @@ export class ActivityService {
// skips the number of results according to page number and number of results per page
const skip = resPerPage * (currentPage - 1);
const activities = this.activityModel
.find({ createdByUser: userId, isArchived: false, isHidden: false })
.sort({ eventDate: 1, _id: 1 })
.limit(resPerPage)
.skip(skip)
.exec();
.find({ createdByUser: userId, isArchived: false, isHidden: false })
.sort({ eventDate: 1, _id: 1 })
.limit(resPerPage)
.skip(skip)
.exec();
return activities;
} catch (error) {
throw new BadRequestException(error.message);
Expand Down Expand Up @@ -198,9 +201,19 @@ export class ActivityService {
if (!activity) {
throw new NotFoundException(`Activity with ID ${id} not found.`);
}
const archivedActivity = await this.activityModel
.findByIdAndUpdate(id, { isArchived: true })
.exec();
// Fetch the current value of isArchived
const currentIsArchived = activity.isArchived;
// Toggle the value of isArchived
const newIsArchived = !currentIsArchived;

// Update the document with the new value of isArchived
const archivedActivity = await this.activityModel
.findByIdAndUpdate(
id,
{ $set: { isArchived: newIsArchived } }, // Set the new value of isArchived
{ new: true },
)
.exec();
return {
archivedActivity,
message: 'Activity archived successfully.',
Expand Down
Loading