Skip to content
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ package-lock.json
yarn.lock
*.DS_Store
*.pem
firebase-admin-service-account.json
firebase-admin-service-account.json
# Firebase secrets
secrets/
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@
"@tensorflow-models/universal-sentence-encoder": "^1.3.3",
"@tensorflow/tfjs": "^4.2.0",
"@tensorflow/tfjs-node": "^4.22.0",
"@types/node-cron": "^3.0.11",
"body-parser": "^1.19.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"cors": "^2.8.6",
"dotenv": "^10.0.0",
"express": "^4.17.2",
"faker": "^5.5.3",
"firebase-admin": "^13.1.0",
"google-auth-library": "^7.14.0",
"math.js": "^1.1.46",
"multer": "1.4.5-lts.1",
"node-cron": "^4.2.1",
"node-fetch": "^2.6.1",
"pg": "^8.7.1",
"pgvector": "^0.2.0",
Expand Down
115 changes: 115 additions & 0 deletions src/api/controllers/AvailabilityController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Body, CurrentUser, Get, JsonController, Param, Post } from 'routing-controllers';
import { UserModel } from '../../models/UserModel';
import { AvailabilityService } from '../../services/AvailabilityService';
import {
UpdateAvailabilityRequest,
GetAvailabilityResponse,
UserAvailabilityResponse
} from '../../types';

@JsonController('availability/')
export class AvailabilityController {
private availabilityService: AvailabilityService;

constructor(availabilityService: AvailabilityService) {
this.availabilityService = availabilityService;
}

/**
* Get availability for the current user
* Creates empty availability if user doesn't have one yet
* GET /availability/
*/
@Get()
async getMyAvailability(
@CurrentUser() user: UserModel
): Promise<GetAvailabilityResponse> {
const availability = await this.availabilityService.getAvailabilityByUserId(
user.firebaseUid
);

return {
availability: this.toResponse(availability)
};
}

/**
* Get availability for a specific user by their userId
* Creates empty availability if user doesn't have one yet
* GET /availability/user/:userId
*/
@Get('user/:userId')
async getAvailabilityByUserId(
@Param('userId') userId: string
): Promise<GetAvailabilityResponse> {
const availability = await this.availabilityService.getAvailabilityByUserId(userId);

return {
availability: this.toResponse(availability)
};
}

/**
* Update availability for specific days
* Only the days included in the request are updated - other days remain unchanged
* Send an empty array for a day to clear that day's availability
*
* POST /availability/update/
*
* Example request body:
* {
* "schedule": {
* "2026-01-23": [
* { "startDate": "2026-01-23T16:00:00Z", "endDate": "2026-01-23T18:00:00Z" }
* ],
* "2026-01-24": [] // clears this day
* }
* }
*/
@Post('update/')
async updateAvailability(
@CurrentUser() user: UserModel,
@Body() request: UpdateAvailabilityRequest
): Promise<GetAvailabilityResponse> {
// Convert string dates to Date objects for each day
const scheduleUpdates: Record<string, { startDate: Date; endDate: Date }[]> = {};

for (const [day, slots] of Object.entries(request.schedule)) {
scheduleUpdates[day] = slots.map(slot => ({
startDate: new Date(slot.startDate),
endDate: new Date(slot.endDate)
}));
}

const availability = await this.availabilityService.updateAvailability(
user.firebaseUid,
scheduleUpdates
);

return {
availability: this.toResponse(availability)
};
}

/**
* Helper to convert internal type to response type
*/
private toResponse(availability: any): UserAvailabilityResponse {
// Convert schedule to response format
const scheduleResponse: Record<string, { startDate: Date; endDate: Date }[]> = {};

for (const [day, slots] of Object.entries(availability.schedule)) {
scheduleResponse[day] = (slots as any[]).map((slot: any) => ({
startDate: slot.startDate,
endDate: slot.endDate
}));
}

return {
id: availability.id,
userId: availability.userId,
schedule: scheduleResponse,
updatedAt: availability.updatedAt
};
}
}
Loading