From d74c259a7d144563c3d47c29484915a9baf35a81 Mon Sep 17 00:00:00 2001 From: Ivin Joel Abraham Date: Sun, 26 Jan 2025 21:04:22 +0530 Subject: [PATCH 1/5] fix streak mutation from throwing an err if record does not exist for a member and update it to not handle discord_id --- src/graphql/mutations/streak_mutations.rs | 66 +++++++---------------- src/models/status_update_streak.rs | 3 +- 2 files changed, 19 insertions(+), 50 deletions(-) diff --git a/src/graphql/mutations/streak_mutations.rs b/src/graphql/mutations/streak_mutations.rs index 8b2728b..2d01d39 100644 --- a/src/graphql/mutations/streak_mutations.rs +++ b/src/graphql/mutations/streak_mutations.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use async_graphql::{Context, Error, Object, Result}; +use async_graphql::{Context, Object, Result}; use sqlx::PgPool; use crate::models::status_update_streak::{StatusUpdateStreak as Streak, StreakInput}; @@ -14,29 +14,15 @@ impl StreakMutations { async fn increment_streak(&self, ctx: &Context<'_>, input: StreakInput) -> Result { let pool = ctx.data::>().expect("Pool must be in context."); - // Ensure at least one identifier is provided - if input.member_id.is_none() && input.discord_id.is_none() { - return Err(Error::new( - "Either `member_id` or `discord_id` must be provided.", - )); - } - - let mut sql = String::from("UPDATE Streaks SET current_streak = current_streak + 1, max_streak = GREATEST(max_streak, current_streak + 1) WHERE "); - if let Some(_) = input.member_id { - sql.push_str("member_id = $1"); - } else if let Some(_) = input.discord_id { - sql.push_str("discord_id = $1"); - } - - sql.push_str(" RETURNING *"); - - let query = if let Some(member_id) = input.member_id { - sqlx::query_as::<_, Streak>(&sql).bind(member_id) - } else if let Some(discord_id) = input.discord_id { - sqlx::query_as::<_, Streak>(&sql).bind(discord_id) - } else { - return Err(Error::new("Invalid input.")); - }; + let query = sqlx::query_as::<_, Streak>( + " + INSERT INTO StatusUpdateStreak VALUES ($1, 1, 1) + ON CONFLICT (member_id) DO UPDATE SET + current_streak = StatusUpdateStreak.current_streak + 1, + max_streak = GREATEST(StatusUpdateStreak.max_streak, StatusUpdateStreak.current_streak + 1) + RETURNING *", + ) + .bind(input.member_id); let updated_streak = query.fetch_one(pool.as_ref()).await?; @@ -46,30 +32,14 @@ impl StreakMutations { async fn reset_streak(&self, ctx: &Context<'_>, input: StreakInput) -> Result { let pool = ctx.data::>().expect("Pool must be in context."); - // Ensure at least one identifier is provided - if input.member_id.is_none() && input.discord_id.is_none() { - return Err(Error::new( - "Either `member_id` or `discord_id` must be provided.", - )); - } - - let mut sql = String::from("UPDATE Streaks SET current_streak = 0 WHERE "); - - if let Some(_) = input.member_id { - sql.push_str("member_id = $1"); - } else if let Some(_) = input.discord_id { - sql.push_str("discord_id = $1"); - } - - sql.push_str(" RETURNING *"); - - let query = if let Some(member_id) = input.member_id { - sqlx::query_as::<_, Streak>(&sql).bind(member_id) - } else if let Some(discord_id) = input.discord_id { - sqlx::query_as::<_, Streak>(&sql).bind(discord_id) - } else { - return Err(Error::new("Invalid input.")); - }; + let query = sqlx::query_as::<_, Streak>( + " + INSERT INTO StatusUpdateStreak VALUES ($1, 0, 0) + ON CONFLICT (member_id) DO UPDATE + SET current_streak = 0 + RETURNING *", + ) + .bind(input.member_id); let updated_streak = query.fetch_one(pool.as_ref()).await?; Ok(updated_streak) diff --git a/src/models/status_update_streak.rs b/src/models/status_update_streak.rs index 5d26f84..d5b279d 100644 --- a/src/models/status_update_streak.rs +++ b/src/models/status_update_streak.rs @@ -18,6 +18,5 @@ pub struct StatusUpdateStreakInfo { /// This struct is used to deserialize the input recieved for mutations on StatusUpdateStreak. #[derive(InputObject)] pub struct StreakInput { - pub member_id: Option, - pub discord_id: Option, + pub member_id: i32, } From 5c6f633b89efa39442b11b7e3c1d255f70b1e3b5 Mon Sep 17 00:00:00 2001 From: Ivin Joel Abraham Date: Mon, 3 Feb 2025 14:00:06 +0530 Subject: [PATCH 2/5] fix streaks incrementing negative streaks --- src/graphql/mutations/streak_mutations.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/graphql/mutations/streak_mutations.rs b/src/graphql/mutations/streak_mutations.rs index 2d01d39..e4ac6d5 100644 --- a/src/graphql/mutations/streak_mutations.rs +++ b/src/graphql/mutations/streak_mutations.rs @@ -35,8 +35,11 @@ impl StreakMutations { let query = sqlx::query_as::<_, Streak>( " INSERT INTO StatusUpdateStreak VALUES ($1, 0, 0) - ON CONFLICT (member_id) DO UPDATE - SET current_streak = 0 + ON CONFLICT (member_id) DO UPDATE + SET current_streak = CASE + WHEN StatusUpdateStreak.current_streak > 0 THEN 0 + ELSE StatusUpdateStreak.current_streak - 1 + END RETURNING *", ) .bind(input.member_id); From c100b6415640eb6b6affe3586489a0f654b1177d Mon Sep 17 00:00:00 2001 From: Hridesh MG Date: Mon, 3 Feb 2025 20:46:50 +0530 Subject: [PATCH 3/5] fix: branch name and cargo init --- .github/workflows/ghcr-deploy.yml | 2 +- Dockerfile | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ghcr-deploy.yml b/.github/workflows/ghcr-deploy.yml index c20d55b..6d15802 100644 --- a/.github/workflows/ghcr-deploy.yml +++ b/.github/workflows/ghcr-deploy.yml @@ -5,7 +5,7 @@ name: Create and publish Docker image to GHCR on: workflow_dispatch: push: - branches: ['master'] + branches: ['main'] jobs: build-and-push-image: diff --git a/Dockerfile b/Dockerfile index c6fa6d6..04c5804 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,10 @@ # Build Stage FROM rust:slim-bullseye AS builder -WORKDIR /build +WORKDIR /builder +RUN cargo init # Compile deps in a separate layer (for caching) COPY Cargo.toml Cargo.lock ./ -# Cargo requires at least one source file for compiling dependencies -RUN mkdir src && echo "fn main() { println!(\"Hello, world!\"); }" > src/main.rs RUN apt-get update RUN apt install -y pkg-config libssl-dev RUN cargo build --release @@ -18,5 +17,5 @@ RUN cargo build --release # Release Stage FROM debian:bullseye-slim AS release -COPY --from=builder /build/target/release/root /usr/local/bin +COPY --from=builder /builder/target/release/root /usr/local/bin CMD ["/usr/local/bin/root"] From bb439099cd6c7370bfe24db7b4b8b8125d9a873c Mon Sep 17 00:00:00 2001 From: Hridesh MG Date: Tue, 4 Feb 2025 18:29:41 +0530 Subject: [PATCH 4/5] docs: add deployment and contributor workflow guidelines --- README.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 65d2e88..dabb19c 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,14 @@ Root is our club's backend, responsible for collecting and distributing data fro GraphQL playground should be available at `http://localhost:8000/graphiql` as long as it's in development mode. + +# Deployment +The deployed instance can be accessed at [root.amfoss.in](https://root.amfoss.in). + +The `main` branch is exclusively meant for production use and commits which get merged into it will make their way into the deployed instance. Active development should occur on the `develop` branch and when sufficient stability has been achieved, they can be merged into `main`. This will kick off the deployment workflow. + +Further implementation details can be found at [bedrock](https://github.com/amfoss/bedrock). + # Documentation See the [documentation](docs/docs.md) for the API reference, database schema and other detailed documentation. @@ -43,9 +51,9 @@ See the [documentation](docs/docs.md) for the API reference, database schema and If you encounter a bug, please check existing issues first to avoid duplicates. If none exist, create a new issue with the following details: -* Title: Concise summary. +* Title: Concise summary. * Description: A detailed description of the issue. -* Steps to Reproduce: If it's a bug, include steps to reproduce. +* Steps to Reproduce: If it's a bug, include steps to reproduce. * Expected and Actual Behavior: Describe what you expected and what actually happened. ## Suggesting Features @@ -62,6 +70,7 @@ If you'd like to fix a bug, add a feature, or improve code quality: * Check the open issues to avoid redundancy. * Open a draft PR if you'd like feedback on an ongoing contribution. +* **Make sure to set the `develop` branch as your pull request target**, see [Deployment](#deployment) ## Coding Standards From eb676db571e84955b05b75869b6913bb53647ffd Mon Sep 17 00:00:00 2001 From: Hridesh MG Date: Tue, 4 Feb 2025 20:40:18 +0530 Subject: [PATCH 5/5] fix: deployment action --- .github/workflows/ghcr-deploy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ghcr-deploy.yml b/.github/workflows/ghcr-deploy.yml index 6d15802..c97f625 100644 --- a/.github/workflows/ghcr-deploy.yml +++ b/.github/workflows/ghcr-deploy.yml @@ -38,7 +38,7 @@ jobs: images: ghcr.io/amfoss/root tags: | # set latest tag for master branch - type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'master') }},priority=2000 + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }},priority=2000 type=schedule,pattern={{date 'YYYYMMDD'}} type=ref,event=tag type=ref,event=pr