/* ScheduleTogether Backend Copyright (C) 2024, Zervó Zadachin DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License version 3 for more details. This program incorporates external libraries for certain functionalities. These libraries are covered by their respective licenses, and their usage agreements are as outlined in their respective documentation or source code. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package accounts import ( "github.com/kataras/iris/v12" db "git.zervo.org/scheduletogether/backend/internal/database" "git.zervo.org/scheduletogether/backend/pkg/types" ) // AccountVerify_Service verifies an account func AccountVerify_Service(ctx iris.Context, req types.AccountVerifyRequest) { // Start a transaction tx := db.Db.Begin() // Search for UUID in pending register verification table var registerVerificationRecord db.RegisterVerification err := tx.Model(&db.RegisterVerification{}).Where("uuid = ?", req.Uuid).First(®isterVerificationRecord).Error if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusBadRequest) ctx.JSON(types.CommonErrorResponse{ Error: "Invalid UUID or verification token", }) return } // Check if provided token matches token in DB if registerVerificationRecord.Token != req.Token { ctx.StatusCode(iris.StatusBadRequest) ctx.JSON(types.CommonErrorResponse{ Error: "Invalid verification token", }) return } // Delete the record once verified err = tx.Delete(®isterVerificationRecord).Error if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "Something went wrong, please try again", }) logboi.Warn("Failed to delete register verification record: " + err.Error()) return } // Update user record in database var userRecord db.User err = tx.Model(&db.User{}).Where("uuid = ?", req.Uuid).Update("verified", true).First(&userRecord).Error if err != nil { tx.Rollback() ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "Something went wrong, please try again", }) logboi.Warn("Failed to update user record: " + err.Error()) return } // Commit the transaction if everything is successful err = tx.Commit().Error if err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.JSON(types.CommonErrorResponse{ Error: "Something went wrong, please try again", }) logboi.Err("Failed to commit transaction: " + err.Error()) return } logboi.Info("Verified user with UUID: " + req.Uuid) ctx.JSON(types.CommonMessageResponse{ Message: "Account verified", }) }