File size: 3,899 Bytes
7b850b7 2207a90 7b850b7 2207a90 7b850b7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import {
Body,
Controller,
Get,
HttpStatus,
Patch,
Post,
Query,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import { CommonServices } from '../shared/common.service';
import { ActivityService } from './activity.service';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { PropertyService } from '../property/property.service';
@Controller('activity')
export class ActivityController extends CommonServices {
constructor(
private readonly activityService: ActivityService,
private readonly propertyService: PropertyService,
) {
super();
}
@Post('create')
@UseGuards(JwtAuthGuard)
async createActivity(@Body() body: any, @Res() res: Response, @Req() req) {
try {
const payload = {
...body,
userId: req.user.userId,
};
const activity = await this.activityService.sharedCreate(payload);
// update views
if (body.action == 'view')
await this.propertyService.sharedFindOneAndUpdate(
{ _id: body.propertyId },
{
$inc: { views: 1 }, // Increment views and add duration
},
{},
);
//update duration/ time spend
if (body.action == 'time_spent')
await this.propertyService.sharedFindOneAndUpdate(
{ _id: body.propertyId },
{
$inc: { total_time_spent: body.duration || 0 }, // Increment views and add duration
},
{},
);
return this.sendResponse(
this.messages.Success,
activity,
HttpStatus.OK,
res,
);
} catch (error) {
console.log(error);
return this.sendResponse(
'Error',
{},
HttpStatus.INTERNAL_SERVER_ERROR,
res,
);
}
}
@Get('')
async getActivityListings(@Res() res: Response, @Req() req): Promise<any> {
try {
const response = await this.activityService.sharedFind({});
return this.sendResponse(
this.messages.Success,
response,
HttpStatus.OK,
res,
);
} catch (error) {
return this.sendResponse(
'Internal server Error',
{},
HttpStatus.INTERNAL_SERVER_ERROR,
res,
);
}
}
// New Update API to modify existing activity
@Patch('update')
@UseGuards(JwtAuthGuard)
async updateActivity(
@Body() body: any, // should include { userId, propertyId, action, duration (optional) }
@Res() res: Response,
@Req() req,
) {
try {
// Check if the activity exists for the user and property
const existingActivity = await this.activityService.sharedFindOne({
userId: body.userId,
propertyId: body.propertyId,
action: body.action, // Example: 'click' or 'view'
});
if (existingActivity) {
// If activity exists, update it
const updatedActivity = await this.activityService.sharedUpdate(
{ _id: existingActivity._id },
{
duration: body.duration ? body.duration : existingActivity.duration,
timestamp: new Date(), // update the timestamp to the latest interaction
},
);
return this.sendResponse(
this.messages.Success,
updatedActivity,
HttpStatus.OK,
res,
);
} else {
// If no activity exists, return a not found response
return this.sendResponse(
'Activity not found',
{},
HttpStatus.NOT_FOUND,
res,
);
}
} catch (error) {
return this.sendResponse(
'Internal server Error',
{},
HttpStatus.INTERNAL_SERVER_ERROR,
res,
);
}
}
}
|