Skip to content

Commit

Permalink
add: time-in time-out calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Wreck-X committed Aug 29, 2024
1 parent c2fcf1f commit 13aadaa
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/graphql/mutations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_graphql::{Context, Object};
use ::chrono::Local;
use chrono::{NaiveDate, NaiveTime};
use sqlx::PgPool;
use sqlx::types::chrono;
Expand Down Expand Up @@ -93,43 +94,47 @@ impl MutationRoot {
let pool = ctx.data::<Arc<PgPool>>().expect("Pool not found in context");

let secret_key = ctx.data::<String>().expect("HMAC secret not found in context");
println!("{}",secret_key);

let mut mac = HmacSha256::new_from_slice(secret_key.as_bytes()).expect("HMAC can take key of any size");

let message = format!("{}{}{}", id, date, is_present);
mac.update(message.as_bytes());

println!("{}", message);
let expected_signature = mac.finalize().into_bytes();

println!("Trying");

// Convert the received HMAC signature from the client to bytes for comparison
let received_signature = hex::decode(hmac_signature)
.map_err(|_| sqlx::Error::Protocol("Invalid HMAC signature".into()))?;

println!("Expected Signature: {:?}", expected_signature);
println!("Received Signature: {:?}", received_signature);
// Check if the signatures match

if expected_signature.as_slice() != received_signature.as_slice() {

return Err(sqlx::Error::Protocol("HMAC verification failed".into()));
}
println!("Success");




let current_time = Local::now().time();

let attendance = sqlx::query_as::<_, Attendance>(
"UPDATE Attendance SET is_present = $1 WHERE id = $2 AND date = $3 RETURNING *"
"
UPDATE Attendance
SET
timein = CASE WHEN timein = '00:00:00' THEN $1 ELSE timein END,
timeout = $1,
is_present = $2
WHERE id = $3 AND date = $4
RETURNING *
"
)


.bind(current_time)
.bind(is_present)
.bind(id)
.bind(date)
.bind(is_present)
.fetch_one(pool.as_ref())
.await?;
.await?;

Ok(attendance)
}
Expand Down

0 comments on commit 13aadaa

Please sign in to comment.