Download api/v1/documents/handler.go from Mrkiti/Mexa: direct link, hf CLI and curl.
- Browser
- Download file 5.77 kB
-
https://huggingface.co/spaces/Mrkiti/Mexa/resolve/main/api/v1/documents/handler.go
- Command line
-
hf download hf://spaces/Mrkiti/Mexa/api/v1/documents/handler.go
-
curl -L -o handler.go https://huggingface.co/spaces/Mrkiti/Mexa/resolve/main/api/v1/documents/handler.go
5.77 kB
| package documents | |
| import ( | |
| "net/http" | |
| "time" | |
| "github.com/gin-gonic/gin" | |
| "mexa/api/v1/db" | |
| "mexa/api/v1/kyc" | |
| "mexa/middleware" | |
| ) | |
| // RegisterRoutes β mounts all document routes | |
| func RegisterRoutes(rg *gin.RouterGroup) { | |
| d := rg.Group("/documents", middleware.RequireAuth()) | |
| { | |
| // MS only β verify listing business docs | |
| d.POST("/listing", | |
| middleware.RequireCapability(middleware.CapabilitySellerListings), | |
| ListingDocs, | |
| ) | |
| // Couriers only β verify permit/insurance | |
| d.POST("/courier", | |
| middleware.RequireRole("NTB", "NTT", "NTM", "NTF", "NTU", "MBL", "MBC"), | |
| CourierDocs, | |
| ) | |
| // Couriers only β get own documents | |
| d.GET("/courier", | |
| middleware.RequireRole("NTB", "NTT", "NTM", "NTF", "NTU", "MBL", "MBC"), | |
| GetCourierDocs, | |
| ) | |
| } | |
| } | |
| // ListingDocs β POST /api/v1/documents/listing | |
| func ListingDocs(c *gin.Context) { | |
| claims, ok := middleware.GetClaims(c) | |
| if !ok { | |
| c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorised"}) | |
| return | |
| } | |
| var body struct { | |
| ProductCode string `json:"product_code" binding:"required"` | |
| DocumentRef string `json:"document_ref" binding:"required"` | |
| } | |
| if err := c.ShouldBindJSON(&body); err != nil { | |
| c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
| return | |
| } | |
| ctx := c.Request.Context() | |
| // VerifyBiometric via KYC cascade β Smile ID | |
| biometricRes, err := kyc.GetAdapter().VerifyBiometric(ctx, kyc.BiometricRequest{ | |
| MyMezaID: claims.UserID, | |
| DocumentRef: body.DocumentRef, | |
| }) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "document verification failed"}) | |
| return | |
| } | |
| // Store smile_job_id and confidence_value only β no raw document data | |
| _, err = db.DB.ExecContext(ctx, ` | |
| UPDATE listings | |
| SET smile_job_id = $1, confidence_value = $2, document_status = 'VERIFIED', updated_at = $3 | |
| WHERE product_code = $4 AND seller_mymeza_id = $5 | |
| `, biometricRes.JobID, biometricRes.ConfidenceValue, time.Now().UTC(), body.ProductCode, claims.UserID) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update listing document status"}) | |
| return | |
| } | |
| // Notify XA β insert notification | |
| db.DB.ExecContext(ctx, ` | |
| INSERT INTO cbk_audit_log (event_type, actor_mymeza_id, actor_role, event_id, amount_kes, description, timestamp) | |
| VALUES ('DOCUMENT_SUBMITTED', $1, 'MS', '', 0, $2, NOW()) | |
| `, claims.UserID, "Listing document submitted β product: "+body.ProductCode) | |
| c.JSON(http.StatusOK, gin.H{ | |
| "message": "document verified", | |
| "smile_job_id": biometricRes.JobID, | |
| "confidence_value": biometricRes.ConfidenceValue, | |
| }) | |
| } | |
| // CourierDocs β POST /api/v1/documents/courier | |
| func CourierDocs(c *gin.Context) { | |
| claims, ok := middleware.GetClaims(c) | |
| if !ok { | |
| c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorised"}) | |
| return | |
| } | |
| var body struct { | |
| DocumentType string `json:"document_type" binding:"required"` | |
| DocumentRef string `json:"document_ref" binding:"required"` | |
| } | |
| if err := c.ShouldBindJSON(&body); err != nil { | |
| c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | |
| return | |
| } | |
| ctx := c.Request.Context() | |
| // VerifyBiometric via KYC cascade β Smile ID | |
| biometricRes, err := kyc.GetAdapter().VerifyBiometric(ctx, kyc.BiometricRequest{ | |
| MyMezaID: claims.UserID, | |
| DocumentRef: body.DocumentRef, | |
| }) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "document verification failed"}) | |
| return | |
| } | |
| // Upsert β safe to resubmit on renewal | |
| _, err = db.DB.ExecContext(ctx, ` | |
| INSERT INTO courier_documents (courier_mymeza_id, document_type, smile_job_id, confidence_value, status, created_at, updated_at) | |
| VALUES ($1, $2, $3, $4, 'VERIFIED', $5, $5) | |
| ON CONFLICT (courier_mymeza_id, document_type) | |
| DO UPDATE SET smile_job_id = $3, confidence_value = $4, status = 'VERIFIED', updated_at = $5 | |
| `, claims.UserID, body.DocumentType, biometricRes.JobID, biometricRes.ConfidenceValue, time.Now().UTC()) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to store document"}) | |
| return | |
| } | |
| // Notify XA | |
| db.DB.ExecContext(ctx, ` | |
| INSERT INTO cbk_audit_log (event_type, actor_mymeza_id, actor_role, event_id, amount_kes, description, timestamp) | |
| VALUES ('DOCUMENT_SUBMITTED', $1, $2, '', 0, $3, NOW()) | |
| `, claims.UserID, claims.Role, "Courier document submitted β type: "+body.DocumentType) | |
| c.JSON(http.StatusOK, gin.H{ | |
| "message": "document verified", | |
| "smile_job_id": biometricRes.JobID, | |
| "confidence_value": biometricRes.ConfidenceValue, | |
| }) | |
| } | |
| // GetCourierDocs β GET /api/v1/documents/courier | |
| func GetCourierDocs(c *gin.Context) { | |
| claims, ok := middleware.GetClaims(c) | |
| if !ok { | |
| c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorised"}) | |
| return | |
| } | |
| ctx := c.Request.Context() | |
| rows, err := db.DB.QueryContext(ctx, ` | |
| SELECT id, document_type, smile_job_id, confidence_value, status, created_at, updated_at | |
| FROM courier_documents | |
| WHERE courier_mymeza_id = $1 | |
| ORDER BY updated_at DESC | |
| `, claims.UserID) | |
| if err != nil { | |
| c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to load documents"}) | |
| return | |
| } | |
| defer rows.Close() | |
| type Row struct { | |
| ID int `json:"id"` | |
| DocumentType string `json:"document_type"` | |
| SmileJobID string `json:"smile_job_id"` | |
| ConfidenceValue float64 `json:"confidence_value"` | |
| Status string `json:"status"` | |
| CreatedAt string `json:"created_at"` | |
| UpdatedAt string `json:"updated_at"` | |
| } | |
| results := []Row{} | |
| for rows.Next() { | |
| var r Row | |
| if err := rows.Scan(&r.ID, &r.DocumentType, &r.SmileJobID, &r.ConfidenceValue, &r.Status, &r.CreatedAt, &r.UpdatedAt); err != nil { | |
| continue | |
| } | |
| results = append(results, r) | |
| } | |
| c.JSON(http.StatusOK, results) | |
| } | |