step 6 : check distance graphql

This commit is contained in:
sindhu
2024-01-22 17:09:02 +07:00
parent ba1f43ae70
commit e9e4d5bf05
7 changed files with 566 additions and 199 deletions

View File

@@ -92,8 +92,6 @@ func (staff *Staff) LoginAttendance(email string, id_google_sign_in string) (*mo
M_StaffNoHp,
M_CompanyID,
M_CompanyName,
M_CompanyLatitude,
M_CompanyLongitude,
M_StaffIsActive,
M_StaffIsLogin,
M_StaffToken,
@@ -123,8 +121,6 @@ func (staff *Staff) LoginAttendance(email string, id_google_sign_in string) (*mo
&ret.PhoneNumber,
&ret.CompanyID,
&ret.CompanyName,
&ret.CompanyLatitude,
&ret.CompanyLongitude,
&ret.IsActive,
&ret.IsLogin,
&ret.Token,

View File

@@ -0,0 +1,129 @@
package transabsensi
import (
"log"
"strconv"
"com.sismedika.com.absensi/graph/model"
db "com.sismedika.com.absensi/pkg/database"
)
// diambil dari graph/model/struct nya TransAbsensiResponse
type TransAbsensiCheckDistanceResponse model.TransAbsensiCheckDistanceResponse
func (transabsensi *TransAbsensiCheckDistanceResponse) CheckDistance(M_StaffID string, M_CompanyID string, CurrentLatitude string, CurrentLongitude string) (*model.TransAbsensiCheckDistanceResponse, error) {
// inisialisasi
var err error
var ret model.TransAbsensiCheckDistanceResponse
var rMaxDistance int
var rCurrentDistance float64
var rMCompanyLongitude string
var rMCompanyLatitude string
var statusCheck string
q := `SELECT
M_CompanyLatitude,
M_CompanyLongitude,
M_CompanyMaxDistance
FROM m_company
WHERE M_CompanyIsActive = 'Y'
AND M_CompanyID = ?`
row := db.Handle.QueryRow(
q,
M_CompanyID)
db.LogSQL(q)
err = row.Scan(
&rMCompanyLatitude,
&rMCompanyLongitude,
&rMaxDistance,
)
if err != nil {
log.Printf("Error m_company select max distance: %v", err)
log.Printf("Executing query: %s\n", q)
return nil, err
}
// get distance
qCurrentDistance := `SELECT distance_v2(?, ?, ?, ?)`
rowxCurrentDistance := db.Handle.QueryRow(
qCurrentDistance,
CurrentLatitude,
CurrentLongitude,
rMCompanyLatitude,
rMCompanyLongitude,
)
db.LogSQL(qCurrentDistance)
err = rowxCurrentDistance.Scan(
&rCurrentDistance,
)
if err != nil {
log.Printf("Error m_staff select: %v", err)
log.Printf("Executing query: %s\n", qCurrentDistance)
return nil, err
}
// check distance
qCheckDistance := `SELECT IF(distance_v2(?, ?, ?, ?) < ?, 'range', 'not range') AS distance_status`
rowx := db.Handle.QueryRow(
qCheckDistance,
CurrentLatitude,
CurrentLongitude,
rMCompanyLatitude,
rMCompanyLongitude,
rMaxDistance,
)
db.LogSQL(qCheckDistance)
err = rowx.Scan(
&statusCheck,
)
if err != nil {
log.Printf("Error m_staff select: %v", err)
log.Printf("Executing query: %s\n", qCheckDistance)
return nil, err
}
if statusCheck == "range" {
// buat di set isi nya kosong dulu
ret.Status = new(string)
ret.Message = new(string)
ret.Selfie = new(string)
ret.CurrentDistance = new(string)
ret.MaxDistance = new(string)
ret.Unit = new(string)
// di set kan langsung dengan * atau pointer
*ret.Status = "OK"
*ret.Selfie = "FALSE"
*ret.Unit = "m"
*ret.CurrentDistance = strconv.FormatFloat(rCurrentDistance, 'f', -1, 64)
*ret.MaxDistance = strconv.Itoa(rMaxDistance)
*ret.Message = "Masih dalam jarak absensi"
}
if statusCheck != "range" {
// buat di set isi nya kosong dulu
ret.Status = new(string)
ret.Message = new(string)
ret.Selfie = new(string)
ret.CurrentDistance = new(string)
ret.MaxDistance = new(string)
ret.Unit = new(string)
// di set kan langsung dengan * atau pointer
*ret.Status = "OK"
*ret.Selfie = "TRUE"
*ret.Unit = "m"
*ret.CurrentDistance = strconv.FormatFloat(rCurrentDistance, 'f', -1, 64)
*ret.MaxDistance = strconv.Itoa(rMaxDistance)
*ret.Message = "Max Distance : " + strconv.Itoa(rMaxDistance) + " meter. Current Distance: " + strconv.FormatFloat(rCurrentDistance, 'f', -1, 64) + ". Jauh dari area absensi, perlu foto selfie"
}
// hasil return nya
return &ret, err
}