Skip to content

Commit b324bf7

Browse files
authored
Merge pull request #52 from vernu/dev
Dev
2 parents 9d748d9 + 9a1fff7 commit b324bf7

File tree

6 files changed

+425
-12
lines changed

6 files changed

+425
-12
lines changed

api/src/gateway/gateway.controller.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,20 @@ export class GatewayController {
113113

114114
@ApiOperation({ summary: 'Get received SMS from a device' })
115115
@ApiResponse({ status: 200, type: RetrieveSMSResponseDTO })
116+
@ApiQuery({ name: 'page', required: false, type: Number, description: 'Page number (default: 1)' })
117+
@ApiQuery({ name: 'limit', required: false, type: Number, description: 'Number of items per page (default: 50, max: 100)' })
116118
@UseGuards(AuthGuard, CanModifyDevice)
117119
// deprecate getReceivedSMS route in favor of get-received-sms
118120
@Get(['/devices/:id/getReceivedSMS', '/devices/:id/get-received-sms'])
119121
async getReceivedSMS(
120122
@Param('id') deviceId: string,
123+
@Request() req,
121124
): Promise<RetrieveSMSResponseDTO> {
122-
const data = await this.gatewayService.getReceivedSMS(deviceId)
123-
return { data }
125+
// Extract page and limit from query params, with defaults and max values
126+
const page = req.query.page ? parseInt(req.query.page, 10) : 1;
127+
const limit = req.query.limit ? Math.min(parseInt(req.query.limit, 10), 100) : 50;
128+
129+
const result = await this.gatewayService.getReceivedSMS(deviceId, page, limit)
130+
return result;
124131
}
125132
}

api/src/gateway/gateway.dto.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,48 @@ export class RetrieveSMSDTO {
195195
updatedAt: Date
196196
}
197197

198+
export class PaginationMetaDTO {
199+
@ApiProperty({
200+
type: Number,
201+
required: true,
202+
description: 'Current page number',
203+
})
204+
page: number;
205+
206+
@ApiProperty({
207+
type: Number,
208+
required: true,
209+
description: 'Number of items per page',
210+
})
211+
limit: number;
212+
213+
@ApiProperty({
214+
type: Number,
215+
required: true,
216+
description: 'Total number of items',
217+
})
218+
total: number;
219+
220+
@ApiProperty({
221+
type: Number,
222+
required: true,
223+
description: 'Total number of pages',
224+
})
225+
totalPages: number;
226+
}
227+
198228
export class RetrieveSMSResponseDTO {
199229
@ApiProperty({
200230
type: [RetrieveSMSDTO],
201231
required: true,
202232
description: 'The received SMS data',
203233
})
204234
data: RetrieveSMSDTO[]
235+
236+
@ApiProperty({
237+
type: PaginationMetaDTO,
238+
required: true,
239+
description: 'Pagination metadata',
240+
})
241+
meta?: PaginationMetaDTO
205242
}

api/src/gateway/gateway.service.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ export class GatewayService {
437437
return sms
438438
}
439439

440-
async getReceivedSMS(deviceId: string): Promise<RetrieveSMSDTO[]> {
440+
async getReceivedSMS(deviceId: string, page = 1, limit = 50): Promise<{ data: any[], meta: any }> {
441441
const device = await this.deviceModel.findById(deviceId)
442442

443443
if (!device) {
@@ -450,20 +450,47 @@ export class GatewayService {
450450
)
451451
}
452452

453+
// Calculate skip value for pagination
454+
const skip = (page - 1) * limit;
455+
456+
// Get total count for pagination metadata
457+
const total = await this.smsModel.countDocuments({
458+
device: device._id,
459+
type: SMSType.RECEIVED,
460+
});
461+
453462
// @ts-ignore
454-
return await this.smsModel
463+
const data = await this.smsModel
455464
.find(
456465
{
457466
device: device._id,
458467
type: SMSType.RECEIVED,
459468
},
460469
null,
461-
{ sort: { receivedAt: -1 }, limit: 200 },
470+
{
471+
sort: { receivedAt: -1 },
472+
limit: limit,
473+
skip: skip
474+
},
462475
)
463476
.populate({
464477
path: 'device',
465478
select: '_id brand model buildId enabled',
466479
})
480+
.lean() // Use lean() to return plain JavaScript objects instead of Mongoose documents
481+
482+
// Calculate pagination metadata
483+
const totalPages = Math.ceil(total / limit);
484+
485+
return {
486+
meta: {
487+
page,
488+
limit,
489+
total,
490+
totalPages,
491+
},
492+
data,
493+
};
467494
}
468495

469496
async getStatsForUser(user: User) {

api/src/gateway/schemas/sms.schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ export class SMS {
6262
}
6363

6464
export const SMSSchema = SchemaFactory.createForClass(SMS)
65+
66+
67+
SMSSchema.index({ device: 1, type: 1, receivedAt: -1 })

0 commit comments

Comments
 (0)