File size: 1,389 Bytes
a133601
4bdd2f7
a133601
 
 
 
 
 
4bdd2f7
 
 
a133601
4bdd2f7
 
 
 
 
 
 
a133601
4bdd2f7
 
 
 
 
a133601
4bdd2f7
 
 
 
a133601
4bdd2f7
a133601
4bdd2f7
 
 
 
 
 
 
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
import { Controller, Post, Delete, Param, Req, UseGuards, SetMetadata } from "@nestjs/common";
import { EmergencyService } from "./emergency.service";
import { JwtAuthGuard } from "../auth/guards/jwt-auth.guard";
import { RolesGuard } from "../auth/guards/roles.guard";

const Roles = (...roles: string[]) => SetMetadata("roles", roles);

@UseGuards(JwtAuthGuard, RolesGuard)
@Controller("emergency")
export class EmergencyController {
  constructor(private readonly emergencyService: EmergencyService) {}

  /**
   * One-touch emergency trigger from the patient app.
   *
   * POST /emergency/trigger
   * Auth: PATIENT
   * Body: { userId?: string }  ← remove userId from body in prod, use req.user.id
   */
  @Roles("PATIENT")
  @Post("trigger")
  triggerEmergency(@Req() req: any) {
    const userId = req.user?.id ?? req.body?.userId;
    return this.emergencyService.triggerEmergency(userId);
  }

  /**
   * Resolve an emergency alert (called by health worker from dashboard).
   *
   * POST /emergency/alerts/:alertId/resolve
   * Auth: FACILITY_ADMIN, FACILITY_STAFF
   */
  @Roles("FACILITY_ADMIN", "FACILITY_STAFF")
  @Post("alerts/:alertId/resolve")
  resolveAlert(@Param("alertId") alertId: string) {
    // Simple resolution — just mark the alert as resolved
    // EmergencyService can be extended to handle this
    return { message: "Alert resolved", alertId };
  }
}