|
|
@@ -1,13 +1,18 @@
|
|
|
import { Injectable, OnModuleInit, Inject } from '@nestjs/common';
|
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
|
|
|
|
+export interface Employee {
|
|
|
+ name: string;
|
|
|
+ photoUrl?: string;
|
|
|
+}
|
|
|
+
|
|
|
@Injectable()
|
|
|
export class FaceService implements OnModuleInit {
|
|
|
private faceGrpc: any;
|
|
|
|
|
|
constructor(
|
|
|
@Inject('FACE_RECOGNITION_PACKAGE')
|
|
|
- private readonly client: ClientGrpc, // ✅ correct type
|
|
|
+ private readonly client: ClientGrpc,
|
|
|
) { }
|
|
|
|
|
|
onModuleInit() {
|
|
|
@@ -15,10 +20,46 @@ export class FaceService implements OnModuleInit {
|
|
|
}
|
|
|
|
|
|
async recognizeFace(imageBuffer: Buffer) {
|
|
|
- return this.faceGrpc.Recognize({ image: imageBuffer }).toPromise();
|
|
|
+ const res = await this.faceGrpc.Recognize({ image: imageBuffer }).toPromise();
|
|
|
+
|
|
|
+ let photoBase64: string | null = null;
|
|
|
+ if (res.image && res.image.length > 0) {
|
|
|
+ photoBase64 = `data:image/jpeg;base64,${Buffer.from(res.image).toString('base64')}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ name: res.name,
|
|
|
+ confidence: res.confidence,
|
|
|
+ photoUrl: photoBase64,
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
async enrollFace(imageBuffer: Buffer, name: string) {
|
|
|
return this.faceGrpc.EnrollFace({ image: imageBuffer, name }).toPromise();
|
|
|
}
|
|
|
+
|
|
|
+ async getAllEmployees(): Promise<Employee[]> {
|
|
|
+ // Get names + images from gRPC
|
|
|
+ const res = await this.faceGrpc.GetAllEmployees({}).toPromise();
|
|
|
+
|
|
|
+ // Map each employee to include base64 photo URL
|
|
|
+ return res.employees.map((emp: { name: string; image: Buffer }) => {
|
|
|
+ let photoUrl: string | null = null;
|
|
|
+ if (emp.image && emp.image.length > 0) {
|
|
|
+ photoUrl = `data:image/jpeg;base64,${Buffer.from(emp.image).toString('base64')}`;
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ name: emp.name,
|
|
|
+ photoUrl,
|
|
|
+ };
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ async deleteEmployee(name: string): Promise<{ success: boolean; message: string }> {
|
|
|
+ const res = await this.faceGrpc.DeleteEmployee({ name }).toPromise();
|
|
|
+ return {
|
|
|
+ success: res.success,
|
|
|
+ message: res.message,
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|