Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions fastify/assets/@types/fastify.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'fastify';
import { BrowsingFilter, BrowsingSort } from '../srcs/services/BrowsingService';

declare module 'fastify' {
interface FastifyInstance {
Expand Down Expand Up @@ -104,6 +105,10 @@ declare module 'fastify' {
reportUser(reportedId: number, reporterId: number): Promise<void>;
};

browsingService: {
browseUsers(userId: number, limit: number = 5, offset: number = 0, radius: number = 25, filters?: BrowsingFilter, sort?: BrowsingSort): Promise<Array<any>>
};

nodemailer: any;

authenticate(request: any, reply: any): Promise<void>;
Expand Down
3 changes: 3 additions & 0 deletions fastify/assets/srcs/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import chatServicePlugin from './services/ChatService'
import mailServicePlugin from './services/MailService'
import notificationService from './services/NotificationsServices'
import reportService from './services/ReportService'
import browsingService from './services/BrowsingService'
// import custom plugins
import authenticate from './plugins/authenticate'
import checkImageConformity from './plugins/checkImageConformity'
Expand Down Expand Up @@ -73,6 +74,8 @@ export const buildApp = () => {

app.register(reportService);

app.register(browsingService);

app.register(pg, {
connectionString: process.env.PG
});
Expand Down
46 changes: 46 additions & 0 deletions fastify/assets/srcs/controllers/private/browsing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FastifyRequest, FastifyReply } from 'fastify';
import { AppError, UnauthorizedError } from '../../../utils/error';
import { BrowsingFilter, BrowsingUser, BrowsingSort } from '../../../services/BrowsingService';

export const browseUsersHandler = async (request: FastifyRequest, reply: FastifyReply) => {
try {
const {
minAge,
maxAge,
minFame,
maxFame,
tags,
lat,
lng,
radius,
sortBy,
offset,
limit
} = request.params as { minAge: number, maxAge: number, minFame: number, maxFame: number, tags: string, lat: number, lng: number, radius: number, sortBy: string, offset: number, limit: number };

if (!request.user?.id)
throw new UnauthorizedError();
const tagsArray = tags ? tags.split(',') : [];
let requestFilters: BrowsingFilter = {
age: { min: minAge, max: maxAge },
fameRate: { min: minFame, max: maxFame },
tags: tagsArray
};
if (!(lat < -90 || lat > 90 || lng < -180 || lng > 180))
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid location check logic: The condition !(lat < -90 || lat > 90 || lng < -180 || lng > 180) is incorrect. It will set the location even when latitude is 1000 (which is outside the valid range of -90 to 90). The condition should be: lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180.

Suggested change
if (!(lat < -90 || lat > 90 || lng < -180 || lng > 180))
if (lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180)

Copilot uses AI. Check for mistakes.
requestFilters.location = { latitude: lat, longitude: lng };
const users: BrowsingUser[] = await request.server.browsingService.browseUsers(
request.user.id,
limit || 20,
offset || 0,
radius,
requestFilters,
(sortBy ? sortBy as BrowsingSort : undefined)
);
return reply.status(200).send({ users });
} catch (error) {
if (error instanceof AppError) {
return reply.status(error.statusCode).send({ error: error.message });
}
return reply.status(500).send({ error: 'Internal Server Error' });
}
}
32 changes: 25 additions & 7 deletions fastify/assets/srcs/controllers/private/me/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@ export const setProfileHandler = async (
request: FastifyRequest,
reply: FastifyReply
) => {
const body = request.body as any;
const updateObject: any = {};
Object.entries(body).forEach(([key, value]) => {
if (value !== undefined && key !== 'location') {
updateObject[key] = value;
}
});
const body = request.body as {
bio?: string;
tags?: Array<string>;
gender?: string;
orientation?: string;
bornAt?: string;
location?: {
latitude?: number;
longitude?: number;
};
};

let updateObject: {
bio?: string;
tags?: string[];
gender?: string;
orientation?: string;
bornAt?: Date;
} = {};

if (body.bio !== undefined) updateObject.bio = body.bio;
if (body.tags !== undefined) updateObject.tags = body.tags;
if (body.gender !== undefined) updateObject.gender = body.gender;
if (body.orientation !== undefined) updateObject.orientation = body.orientation;
if (body.bornAt) updateObject.bornAt = new Date(body.bornAt);

try {
const user = request.user as FastifyRequestUser | undefined;
Expand Down
65 changes: 65 additions & 0 deletions fastify/assets/srcs/routes/private/browsing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { FastifyInstance } from 'fastify';
import { browseUsersHandler } from '../../../controllers/private/browsing'

export default async function browsingRoutes(fastify: FastifyInstance) {
fastify.get('/:minAge/:maxAge/:minFame/:maxFame/:tags/:lat/:lng/:radius/:sortBy', {
schema: {
params: {
type: 'object',
properties: {
minAge: { type: 'number', minimum: 18, maximum: 100 },
maxAge: { type: 'number', minimum: 18, maximum: 100 },
minFame: { type: 'number', minimum: 0, maximum: 1000 },
maxFame: { type: 'number', minimum: 0, maximum: 1000 },
tags: { type: 'string' }, // comma separated tags
lat: { type: 'number' },
lng: { type: 'number' },
radius: { type: 'number', minimum: 0 },
sortBy: { type: 'string', enum: ['distance', 'age', 'fameRate', 'tags', 'default'] },
offset: { type: 'number', minimum: 0 },
limit: { type: 'number', minimum: 1, maximum: 30 }
},
required: ['minAge', 'maxAge', 'minFame', 'maxFame', 'tags', 'lat', 'lng', 'radius', 'sortBy'],
additionalProperties: false
},
response: {
200: {
type: 'object',
properties: {
users: { type: 'array', items: {
type: 'object',
properties: {
id: { type: 'number' },
firstName: { type: 'string' },
gender: { type: 'string' },
tags: { type: 'array', items: { type: 'string' } },
fameRate: { type: 'number' },
profilePicture: { type: 'string' },
bornAt: { type: 'string' },
distance: { type: 'number' }
},
required: ['id', 'firstName', 'gender', 'tags', 'fameRate', 'profilePicture', 'bornAt', 'distance'],
additionalProperties: false
} }
},
additionalProperties: false
},
400: {
type: 'object',
properties: {
error: { type: 'string', }
},
additionalProperties: false
},
500: {
type: 'object',
properties: {
error: { type: 'string' }
},
additionalProperties: false
}
}
},
handler: browseUsersHandler
});
}
2 changes: 2 additions & 0 deletions fastify/assets/srcs/routes/private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import userRoutes from './user';
import wsRoutes from './ws';
import notificationsRoutes from './notifications';
import reportRoutes from './report';
import browsingRoutes from './browsing';
import statics from '@fastify/static';
import path from 'path';

Expand All @@ -17,6 +18,7 @@ export default async function privateRoutes(fastify: FastifyInstance, options: F
fastify.register(userRoutes, { prefix: '/user' });
fastify.register(notificationsRoutes, { prefix: '/notifications' });
fastify.register(reportRoutes, { prefix: '/report', preHandler: fastify.checkIsCompleted });
fastify.register(browsingRoutes, { prefix: '/browsing', preHandler: fastify.checkIsCompleted });
fastify.register(wsRoutes, { prefix: '/ws', preHandler: fastify.checkIsCompleted });
fastify.register(statics, {
root: path.join(__dirname, '../../../uploads'),
Expand Down
2 changes: 1 addition & 1 deletion fastify/assets/srcs/routes/private/user/me/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { setProfileHandler, getProfileHandler } from "../../../../controllers/pr
const profileRoutes = async (fastify: FastifyInstance) => {
fastify.put('/', {
schema: {

body: {
type: 'object',
properties: {
Expand All @@ -18,7 +19,6 @@ const profileRoutes = async (fastify: FastifyInstance) => {
latitude: { type: 'number' },
longitude: { type: 'number' }
},
required: ['latitude', 'longitude'],
additionalProperties: false
}
},
Expand Down
Loading
Loading