Implement pagination for host activity retrieval in HostService and MinglarService
This commit is contained in:
@@ -4,7 +4,7 @@ import { PrismaService } from '../../../../../common/database/prisma.service';
|
||||
import { safeHandler } from '../../../../../common/utils/handlers/safeHandler';
|
||||
import ApiError from '../../../../../common/utils/helper/ApiError';
|
||||
import { HostService } from '../../../services/host.service';
|
||||
import { string } from 'zod';
|
||||
import { paginationService } from '../../../../../common/utils/pagination/pagination.service';
|
||||
|
||||
|
||||
const prismaService = new PrismaService();
|
||||
@@ -29,11 +29,18 @@ export const handler = safeHandler(async (
|
||||
// Verify token and get user info
|
||||
const userInfo = await verifyHostToken(token);
|
||||
|
||||
// Get pagination params from event
|
||||
const paginationParams = paginationService.getPaginationFromEvent(event);
|
||||
const paginationOptions = paginationService.parsePaginationParams(paginationParams);
|
||||
|
||||
// Read optional search query (supports ?search= or ?q=)
|
||||
const search = event.queryStringParameters?.search || event.queryStringParameters?.q || undefined;
|
||||
|
||||
const data = await hostService.getAllHostActivity(search ? String(search) : undefined, Number(userInfo.id));
|
||||
const result = await hostService.getAllHostActivity(
|
||||
search ? String(search) : undefined,
|
||||
Number(userInfo.id),
|
||||
paginationOptions
|
||||
);
|
||||
|
||||
|
||||
return {
|
||||
@@ -45,8 +52,7 @@ export const handler = safeHandler(async (
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'Data retrieved successfully',
|
||||
data,
|
||||
|
||||
...result,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -384,35 +384,43 @@ export class HostService {
|
||||
});
|
||||
}
|
||||
|
||||
async getAllHostActivity(search?: string, user_xid?: number) {
|
||||
async getAllHostActivity(search?: string, user_xid?: number, paginationOptions?: { page: number; limit: number; skip: number }) {
|
||||
const hostDetails = await this.prisma.hostHeader.findFirst({
|
||||
where: { userXid: user_xid, isActive: true }
|
||||
})
|
||||
|
||||
const hostAllActivities = await this.prisma.activities.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
hostXid: hostDetails.id,
|
||||
},
|
||||
include: {
|
||||
ActivitiesMedia: true,
|
||||
ActivityAmDetails: {
|
||||
select: {
|
||||
accountManager: {
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
profileImage: true,
|
||||
emailAddress: true,
|
||||
roleXid: true,
|
||||
const whereClause = {
|
||||
isActive: true,
|
||||
hostXid: hostDetails.id,
|
||||
};
|
||||
|
||||
const [hostAllActivities, totalCount] = await Promise.all([
|
||||
this.prisma.activities.findMany({
|
||||
where: whereClause,
|
||||
include: {
|
||||
ActivitiesMedia: true,
|
||||
ActivityAmDetails: {
|
||||
select: {
|
||||
accountManager: {
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
profileImage: true,
|
||||
emailAddress: true,
|
||||
roleXid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
activityType: true,
|
||||
},
|
||||
activityType: true,
|
||||
},
|
||||
});
|
||||
skip: paginationOptions?.skip || 0,
|
||||
take: paginationOptions?.limit || 10,
|
||||
orderBy: { id: 'desc' },
|
||||
}),
|
||||
this.prisma.activities.count({ where: whereClause }),
|
||||
]);
|
||||
|
||||
for (const activity of hostAllActivities) {
|
||||
|
||||
@@ -452,8 +460,12 @@ export class HostService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return hostAllActivities;
|
||||
const { paginationService } = require('@/common/utils/pagination/pagination.service');
|
||||
return paginationService.createPaginatedResponse(
|
||||
hostAllActivities,
|
||||
totalCount,
|
||||
paginationOptions || { page: 1, limit: 10, skip: 0 }
|
||||
);
|
||||
}
|
||||
|
||||
async acceptMinglarAgreement(user_xid: number) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import { safeHandler } from '../../../../../common/utils/handlers/safeHandler';
|
||||
import ApiError from '../../../../../common/utils/helper/ApiError';
|
||||
import { PrePopulateService } from '../../../../prepopulate/services/prepopulate.service';
|
||||
import { MinglarService } from '../../../services/minglar.service';
|
||||
import { paginationService } from '../../../../../common/utils/pagination/pagination.service';
|
||||
|
||||
const prismaService = new PrismaService();
|
||||
const minglarService = new MinglarService(prismaService);
|
||||
@@ -30,11 +31,18 @@ export const handler = safeHandler(async (
|
||||
|
||||
const hostXid = Number(event.pathParameters?.id)
|
||||
|
||||
// Get pagination params from event
|
||||
const paginationParams = paginationService.getPaginationFromEvent(event);
|
||||
const paginationOptions = paginationService.parsePaginationParams(paginationParams);
|
||||
|
||||
// Read optional search query (supports ?search= or ?q=)
|
||||
const search = event.queryStringParameters?.search || event.queryStringParameters?.q || undefined;
|
||||
|
||||
const data = await minglarService.getAllHostActivityForMinglar(search ? String(search) : undefined, hostXid);
|
||||
const result = await minglarService.getAllHostActivityForMinglar(
|
||||
search ? String(search) : undefined,
|
||||
hostXid,
|
||||
paginationOptions
|
||||
);
|
||||
|
||||
|
||||
return {
|
||||
@@ -46,7 +54,7 @@ export const handler = safeHandler(async (
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'Data retrieved successfully',
|
||||
data,
|
||||
...result,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -255,38 +255,46 @@ export class MinglarService {
|
||||
});
|
||||
}
|
||||
|
||||
async getAllHostActivityForMinglar(search?: string, hostXid?: number) {
|
||||
const hostActivities = await this.prisma.activities.findMany({
|
||||
where: {
|
||||
isActive: true,
|
||||
...(hostXid ? { hostXid } : {}), // Add only if provided
|
||||
},
|
||||
include: {
|
||||
ActivitiesMedia: {
|
||||
select: {
|
||||
id: true,
|
||||
mediaFileName: true,
|
||||
mediaType: true,
|
||||
displayOrder: true,
|
||||
async getAllHostActivityForMinglar(search?: string, hostXid?: number, paginationOptions?: { page: number; limit: number; skip: number }) {
|
||||
const whereClause = {
|
||||
isActive: true,
|
||||
...(hostXid ? { hostXid } : {}), // Add only if provided
|
||||
};
|
||||
|
||||
const [hostActivities, totalCount] = await Promise.all([
|
||||
this.prisma.activities.findMany({
|
||||
where: whereClause,
|
||||
include: {
|
||||
ActivitiesMedia: {
|
||||
select: {
|
||||
id: true,
|
||||
mediaFileName: true,
|
||||
mediaType: true,
|
||||
displayOrder: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
ActivityAmDetails: {
|
||||
select: {
|
||||
accountManager: {
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
profileImage: true,
|
||||
emailAddress: true,
|
||||
roleXid: true,
|
||||
ActivityAmDetails: {
|
||||
select: {
|
||||
accountManager: {
|
||||
select: {
|
||||
id: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
profileImage: true,
|
||||
emailAddress: true,
|
||||
roleXid: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
activityType: true,
|
||||
},
|
||||
activityType: true,
|
||||
},
|
||||
});
|
||||
skip: paginationOptions?.skip || 0,
|
||||
take: paginationOptions?.limit || 10,
|
||||
orderBy: { id: 'desc' },
|
||||
}),
|
||||
this.prisma.activities.count({ where: whereClause }),
|
||||
]);
|
||||
|
||||
// Process each activity
|
||||
for (const activity of hostActivities) {
|
||||
@@ -327,8 +335,12 @@ export class MinglarService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return hostActivities;
|
||||
const { paginationService } = require('@/common/utils/pagination/pagination.service');
|
||||
return paginationService.createPaginatedResponse(
|
||||
hostActivities,
|
||||
totalCount,
|
||||
paginationOptions || { page: 1, limit: 10, skip: 0 }
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user