Complix Integration added
This commit is contained in:
129
src/main/java/com/rit/whatsapp/service/FirebaseService.java
Normal file
129
src/main/java/com/rit/whatsapp/service/FirebaseService.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package com.rit.whatsapp.service;
|
||||
|
||||
import com.google.cloud.firestore.Firestore;
|
||||
import com.google.cloud.firestore.QueryDocumentSnapshot;
|
||||
import com.google.cloud.firestore.QuerySnapshot;
|
||||
import com.google.cloud.storage.Blob;
|
||||
import com.google.cloud.storage.Bucket;
|
||||
import com.rit.whatsapp.model.GrievanceDraft;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import com.google.cloud.Timestamp;
|
||||
|
||||
@Service
|
||||
public class FirebaseService {
|
||||
|
||||
private final Firestore firestore;
|
||||
private final Bucket bucket;
|
||||
|
||||
@Autowired
|
||||
public FirebaseService(@Autowired(required = false) Firestore firestore,
|
||||
@Autowired(required = false) Bucket bucket) {
|
||||
this.firestore = firestore;
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
public List<String> getDepartments(String org) {
|
||||
List<String> deps = new ArrayList<>();
|
||||
if (firestore == null) return deps;
|
||||
try {
|
||||
QuerySnapshot snapshot = firestore.collection("departments")
|
||||
.whereEqualTo("org", org)
|
||||
.get().get();
|
||||
for (QueryDocumentSnapshot doc : snapshot.getDocuments()) {
|
||||
Boolean isDeleted = doc.getBoolean("isDeleted");
|
||||
if (isDeleted == null || !isDeleted) {
|
||||
deps.add(doc.getString("name"));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error fetching departments: " + e.getMessage());
|
||||
}
|
||||
return deps;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getUserGrievances(String imsId) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
if (firestore == null) return list;
|
||||
try {
|
||||
QuerySnapshot snapshot = firestore.collection("grievances")
|
||||
.whereEqualTo("studentRegNo", imsId)
|
||||
.get().get();
|
||||
for (QueryDocumentSnapshot doc : snapshot.getDocuments()) {
|
||||
Map<String, Object> data = new HashMap<>(doc.getData());
|
||||
data.put("id", doc.getId());
|
||||
list.add(data);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error fetching grievances: " + e.getMessage());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public String uploadImage(byte[] imageBytes, String mimeType, String ticketId) {
|
||||
if (bucket == null || imageBytes == null) return null;
|
||||
try {
|
||||
String extension = mimeType != null && mimeType.contains("png") ? ".png" : ".jpg";
|
||||
String blobName = "grievances/" + ticketId + "/" + UUID.randomUUID() + extension;
|
||||
Blob blob = bucket.create(blobName, imageBytes, mimeType != null ? mimeType : "image/jpeg");
|
||||
return blob.signUrl(7, TimeUnit.DAYS).toString();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to upload image to Firebase Storage: " + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void saveGrievance(GrievanceDraft draft) {
|
||||
if (firestore == null) {
|
||||
System.out.println("Firestore not configured.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("userId", "whatsapp_" + draft.getPhoneNumber());
|
||||
data.put("title", draft.getTitle());
|
||||
data.put("description", draft.getDescription());
|
||||
data.put("department", draft.getDepartment());
|
||||
data.put("org", "RIT");
|
||||
data.put("studentName", draft.getStudentName());
|
||||
data.put("studentRegNo", draft.getImsId());
|
||||
data.put("status", draft.getStatus());
|
||||
data.put("createdAt", Timestamp.now());
|
||||
if (draft.getImageUrl() != null) {
|
||||
data.put("imageUrl", draft.getImageUrl());
|
||||
}
|
||||
firestore.collection("grievances").document(draft.getTicketId()).set(data).get();
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to save grievance: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteGrievance(String ticketId) {
|
||||
if (firestore != null) {
|
||||
try {
|
||||
firestore.collection("grievances").document(ticketId).delete().get();
|
||||
System.out.println("Deleted Firestore document: " + ticketId);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to delete Firestore document: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (bucket != null) {
|
||||
try {
|
||||
Iterable<Blob> blobs = bucket.list(com.google.cloud.storage.Storage.BlobListOption.prefix("grievances/" + ticketId + "/")).iterateAll();
|
||||
for (Blob blob : blobs) {
|
||||
blob.delete();
|
||||
System.out.println("Deleted Storage blob: " + blob.getName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to delete Storage blobs: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user