Skip to content

Commit 7d035c4

Browse files
committed
integrated audit into events
modified the events service and controller approve endpoint to accept admin id added more tests for approval event service
1 parent ba09f4e commit 7d035c4

5 files changed

Lines changed: 77 additions & 19 deletions

File tree

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import {
22
Controller,
3-
UseGuards,
4-
Param,
53
Delete,
4+
Param,
65
Patch,
6+
Request,
7+
UseGuards,
78
} from '@nestjs/common';
89
import { ApiBearerAuth, ApiParam, ApiTags } from '@nestjs/swagger';
9-
import { EventService } from './events.users.service';
1010
import { JwtAdminsGuard } from 'src/shared/auth/guards/jwt.admins.guard';
11-
11+
import { ApiReq } from 'src/shared/interfaces';
12+
import { EventService } from './events.users.service';
1213

1314
@ApiTags('admins')
1415
@Controller('admins')
1516
export class EventAdminsController {
16-
constructor(
17-
private readonly eventService: EventService,
18-
) {}
17+
constructor(private readonly eventService: EventService) {}
1918

2019
@ApiBearerAuth()
2120
@UseGuards(JwtAdminsGuard)
2221
@Patch('event/:id/approve')
2322
@ApiParam({ name: 'id', type: 'string' })
24-
async approveEvent(@Param('id') id: string) {
25-
return this.eventService.approveEvent(id);
23+
async approveEvent(@Param('id') id: string, @Request() req: ApiReq) {
24+
const admin_id = req.user._id.toString();
25+
return this.eventService.approveEvent(id, admin_id);
2626
}
2727

2828
@ApiBearerAuth()
@@ -32,5 +32,4 @@ export class EventAdminsController {
3232
async softDeleteEvent(@Param('id') id: string) {
3333
return this.eventService.softDeleteEvent(id);
3434
}
35-
3635
}

src/events/events.service.spec.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ describe('EventService', () => {
6161

6262
notificationsServiceMock = {
6363
createNotification: jest.fn().mockResolvedValue(undefined),
64+
getNotificationByUserId: jest.fn().mockResolvedValue(undefined),
65+
resolveNotification: jest.fn().mockResolvedValue(undefined),
6466
};
6567

6668
const module: TestingModule = await Test.createTestingModule({
@@ -171,15 +173,56 @@ describe('EventService', () => {
171173
});
172174

173175
describe('approveEvent', () => {
174-
it('should approve an event', async () => {
175-
eventModelMock.findByIdAndUpdate.mockResolvedValueOnce(mockEvent); // direct return
176-
const result = await service.approveEvent(mockEvent._id.toString());
176+
it('should approve an event and resolve notification', async () => {
177+
const admin_id = 'admin_id';
178+
const message = 'event approved';
179+
180+
// Mock findByIdAndUpdate to resolve with updated event
181+
eventModelMock.findByIdAndUpdate = jest.fn().mockResolvedValue(mockEvent);
182+
183+
// Mock findById to return the same event
184+
eventModelMock.findById = jest.fn().mockResolvedValue(mockEvent);
185+
186+
// Mock notifications
187+
notificationsServiceMock.getNotificationByUserId = jest
188+
.fn()
189+
.mockResolvedValue({ _id: 'notif_id' });
190+
notificationsServiceMock.resolveNotification = jest
191+
.fn()
192+
.mockResolvedValue(undefined);
193+
194+
const result = await service.approveEvent(
195+
mockEvent._id.toString(),
196+
admin_id,
197+
message,
198+
);
199+
177200
expect(result).toEqual(mockEvent);
178201
expect(eventModelMock.findByIdAndUpdate).toHaveBeenCalledWith(
179202
mockEvent._id.toString(),
180203
{ status: Status.APPROVED },
181204
{ new: true },
182205
);
206+
expect(eventModelMock.findById).toHaveBeenCalledWith({
207+
_id: mockEvent._id.toString(),
208+
});
209+
expect(
210+
notificationsServiceMock.getNotificationByUserId,
211+
).toHaveBeenCalledWith(mockEvent.host, mockEvent._id.toString());
212+
expect(notificationsServiceMock.resolveNotification).toHaveBeenCalledWith(
213+
'notif_id',
214+
admin_id,
215+
message,
216+
'APPROVED',
217+
);
218+
});
219+
220+
it('should throw NotFoundException if event not found', async () => {
221+
eventModelMock.findByIdAndUpdate = jest.fn().mockResolvedValue(null);
222+
223+
await expect(
224+
service.approveEvent(mockEvent._id.toString(), 'admin_id'),
225+
).rejects.toThrow(NotFoundException);
183226
});
184227
});
185228
});

src/events/events.users.controller.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('EventUserController', () => {
1010
let controller: EventUserController;
1111
let service: jest.Mocked<EventService>;
1212

13-
const mockEvent = { id: '1', title: 'Sample Event' };
13+
const mockEvent = { id: '1', title: 'Sample Event', admin_id: 'admin_id' };
1414

1515
afterEach(async () => {
1616
jest.clearAllMocks();

src/events/events.users.service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,34 @@ export class EventService {
102102
return deleteEvent;
103103
}
104104

105-
async approveEvent(id: string): Promise<Event> {
106-
let approveEvent = this.eventModel.findByIdAndUpdate(
105+
async approveEvent(
106+
id: string,
107+
admin_id: string,
108+
message: string = 'event approved',
109+
): Promise<Event> {
110+
const approveEvent = await this.eventModel.findByIdAndUpdate(
107111
id,
108112
{ status: Status.APPROVED },
109113
{ new: true },
110114
);
115+
111116
if (!approveEvent) {
112117
throw new NotFoundException(`Event with ID ${id} not found`);
113118
}
114119

120+
const event = await this.eventModel.findById({ _id: id });
115121
//TODO: move event to audit
116-
122+
const existingNotification =
123+
await this.notificationsService.getNotificationByUserId(
124+
event.host,
125+
event._id.toString(),
126+
);
127+
await this.notificationsService.resolveNotification(
128+
existingNotification._id.toString(),
129+
admin_id,
130+
message,
131+
'APPROVED',
132+
);
117133
return approveEvent;
118134
}
119135
}

src/users/users.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export class UsersService {
498498
userApplication._id.toString(),
499499
userApplication._id.toString(),
500500
);
501-
this.notificationsService.resolveNotification(
501+
await this.notificationsService.resolveNotification(
502502
existingNotification._id.toString(),
503503
admin_id,
504504
message,
@@ -552,7 +552,7 @@ export class UsersService {
552552
userApplication._id.toString(),
553553
userApplication._id.toString(),
554554
);
555-
this.notificationsService.resolveNotification(
555+
await this.notificationsService.resolveNotification(
556556
existingNotification._id.toString(),
557557
admin_id,
558558
message,

0 commit comments

Comments
 (0)