diff --git a/Makefile b/Makefile index a4f0c3e82..bb61acc91 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,18 @@ TOOLS_IMAGE=dwarvesv/fortress-tools:latest APP_ENVIRONMENT=docker run --rm -v ${PWD}:/${APP_NAME} -w /${APP_NAME} --net=host ${TOOLS_IMAGE} SWAGGER_VERSION = v1.16.1 -.PHONY: setup init build dev test migrate-up migrate-down ci +.PHONY: setup shel colima-start colima-stop init build dev test migrate-up migrate-down ci + +shell: + @if ! command -v devbox >/dev/null 2>&1; then curl -fsSL https://get.jetpack.io/devbox | bash; fi + @devbox install + @devbox shell + +colima-start: + colima start --cpu 1 --memory 2 --disk 30 + +colima-stop: + @colima stop setup: docker pull ${TOOLS_IMAGE} diff --git a/README.md b/README.md index b703b14da..154cc9bb6 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,31 @@ This repository is the official BE service for Fortress ## How to contribute -### Prerequisites +### Setup local development environment (pick one of following ways) -1. Go installed -2. Docker installed +#### Using DEVBOX + +1. Create isolated shell using devbox + +``` +make shell +``` + +2. Start Colima as container runtime + +``` +make colima-start +``` + +Related issue when initiating development environment first time using colima +- [docker-credential-desktop not installed or not available in PATH](https://stackoverflow.com/questions/67642620/docker-credential-desktop-not-installed-or-not-available-in-path/72888813#72888813) + - tltr: Cheating by removing `credsStore` in the `~/.docker/config.json`. Or installing `osxkeychain` + +#### Using your machine environment + +1. Install Golang + +2. Install Docker ### How to run source code locally diff --git a/devbox.json b/devbox.json new file mode 100644 index 000000000..02e5578ee --- /dev/null +++ b/devbox.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.10.7/.schema/devbox.schema.json", + "packages": [ + "docker@latest", + "docker-compose@latest", + "git@latest", + "qemu@latest", + "vim@latest", + "colima@latest", + "air@latest", + "go@1.21", + "go-swag@1.8.4" + ], + "shell": { + "scripts": { + "test": [ + "echo \"Error: no test specified\" && exit 1" + ] + } + }, + "env": { + "PATH": "/usr/bin:$PWD/.devbox/nix/profile/default/bin:$PATH", + "GOPATH": "$PWD/.devbox/nix/profile/default/share/go" + } +} diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 000000000..678447557 --- /dev/null +++ b/devbox.lock @@ -0,0 +1,493 @@ +{ + "lockfile_version": "1", + "packages": { + "air@latest": { + "last_modified": "2024-06-08T17:34:36Z", + "resolved": "github:NixOS/nixpkgs/cd18e2ae9ab8e2a0a8d715b60c91b54c0ac35ff9#air", + "source": "devbox-search", + "version": "1.52.2", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/x8hjn9gg5sc5s00czja7ryklphqn4axd-air-1.52.2", + "default": true + } + ], + "store_path": "/nix/store/x8hjn9gg5sc5s00czja7ryklphqn4axd-air-1.52.2" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/2zn6h0lj1d95pahnfpjnwzx15mjphj4w-air-1.52.2", + "default": true + } + ], + "store_path": "/nix/store/2zn6h0lj1d95pahnfpjnwzx15mjphj4w-air-1.52.2" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/3x2prnsfzm082mpbighi8bv4yqvk3mrd-air-1.52.2", + "default": true + } + ], + "store_path": "/nix/store/3x2prnsfzm082mpbighi8bv4yqvk3mrd-air-1.52.2" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/pf6j811r7y3lfmdq0njll7mlzi1d521p-air-1.52.2", + "default": true + } + ], + "store_path": "/nix/store/pf6j811r7y3lfmdq0njll7mlzi1d521p-air-1.52.2" + } + } + }, + "colima@latest": { + "last_modified": "2024-05-29T10:04:41Z", + "resolved": "github:NixOS/nixpkgs/ac82a513e55582291805d6f09d35b6d8b60637a1#colima", + "source": "devbox-search", + "version": "0.6.9", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/y4ippqqkqr8ajj6svd9am0nvqxcxyqpa-colima-0.6.9", + "default": true + } + ], + "store_path": "/nix/store/y4ippqqkqr8ajj6svd9am0nvqxcxyqpa-colima-0.6.9" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/nbbfnidysbvxwx501s219a5lwpxggrr7-colima-0.6.9", + "default": true + } + ], + "store_path": "/nix/store/nbbfnidysbvxwx501s219a5lwpxggrr7-colima-0.6.9" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/5ri749ngr6sclwffcswnigz9j69rh21i-colima-0.6.9", + "default": true + } + ], + "store_path": "/nix/store/5ri749ngr6sclwffcswnigz9j69rh21i-colima-0.6.9" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/p4z5kjymazdf8nh23vr6pgridiwnmbpn-colima-0.6.9", + "default": true + } + ], + "store_path": "/nix/store/p4z5kjymazdf8nh23vr6pgridiwnmbpn-colima-0.6.9" + } + } + }, + "docker-compose@latest": { + "last_modified": "2024-05-22T06:18:38Z", + "resolved": "github:NixOS/nixpkgs/3f316d2a50699a78afe5e77ca486ad553169061e#docker-compose", + "source": "devbox-search", + "version": "2.27.0", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/pgqxb2dgahlwx9pqdmqav9znv8f6zwms-docker-compose-2.27.0", + "default": true + } + ], + "store_path": "/nix/store/pgqxb2dgahlwx9pqdmqav9znv8f6zwms-docker-compose-2.27.0" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/79arf5qrapdrvqavd1hafr1mbskz2bjp-docker-compose-2.27.0", + "default": true + } + ], + "store_path": "/nix/store/79arf5qrapdrvqavd1hafr1mbskz2bjp-docker-compose-2.27.0" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/lnbvsc4zn0q818xgpg2mq45hyvzlk77n-docker-compose-2.27.0", + "default": true + } + ], + "store_path": "/nix/store/lnbvsc4zn0q818xgpg2mq45hyvzlk77n-docker-compose-2.27.0" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/9sdw2ng378133mr3ld04b0rac1hwkpm8-docker-compose-2.27.0", + "default": true + } + ], + "store_path": "/nix/store/9sdw2ng378133mr3ld04b0rac1hwkpm8-docker-compose-2.27.0" + } + } + }, + "docker@latest": { + "last_modified": "2024-05-22T06:18:38Z", + "resolved": "github:NixOS/nixpkgs/3f316d2a50699a78afe5e77ca486ad553169061e#docker", + "source": "devbox-search", + "version": "24.0.9", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/xpg0zv3r2pjz895cx95av82rd8453wqi-docker-24.0.9", + "default": true + } + ], + "store_path": "/nix/store/xpg0zv3r2pjz895cx95av82rd8453wqi-docker-24.0.9" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/sqj9fri6jclyv43hghgq1b0jl92rwnxw-docker-24.0.9", + "default": true + } + ], + "store_path": "/nix/store/sqj9fri6jclyv43hghgq1b0jl92rwnxw-docker-24.0.9" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/q2jyz2n42gpbk48vhbbc7q16wwzxprn2-docker-24.0.9", + "default": true + } + ], + "store_path": "/nix/store/q2jyz2n42gpbk48vhbbc7q16wwzxprn2-docker-24.0.9" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/6ahh5vkyrjjk9k32lhxi809z8fv82qgb-docker-24.0.9", + "default": true + } + ], + "store_path": "/nix/store/6ahh5vkyrjjk9k32lhxi809z8fv82qgb-docker-24.0.9" + } + } + }, + "git@latest": { + "last_modified": "2024-05-22T06:18:38Z", + "resolved": "github:NixOS/nixpkgs/3f316d2a50699a78afe5e77ca486ad553169061e#git", + "source": "devbox-search", + "version": "2.44.1", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/1y3m89x5sl3bwag9lk4fdbqmswzjp9is-git-2.44.1", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/hhwbgxadc1r7f2vf0f7mf4mlcy3xsxh9-git-2.44.1-doc" + } + ], + "store_path": "/nix/store/1y3m89x5sl3bwag9lk4fdbqmswzjp9is-git-2.44.1" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/y6si9acv2p32jjyavs5arj8lwk7qq945-git-2.44.1", + "default": true + }, + { + "name": "debug", + "path": "/nix/store/vjgwf4vr6dqclvb503fqir13yiqp7bf6-git-2.44.1-debug" + }, + { + "name": "doc", + "path": "/nix/store/1c5816l3n7dgbjkbf6fdlw0amq5rvazq-git-2.44.1-doc" + } + ], + "store_path": "/nix/store/y6si9acv2p32jjyavs5arj8lwk7qq945-git-2.44.1" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/rwkir7nma4hgyg8lydzjisbjgwr9a4xf-git-2.44.1", + "default": true + }, + { + "name": "doc", + "path": "/nix/store/y4l5l7k5syy0s3baznixgxbnmn8aiyxq-git-2.44.1-doc" + } + ], + "store_path": "/nix/store/rwkir7nma4hgyg8lydzjisbjgwr9a4xf-git-2.44.1" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/jxjzgz1p0hdqsg8k17ivw5mgcd6ffx9c-git-2.44.1", + "default": true + }, + { + "name": "debug", + "path": "/nix/store/93xf345jmvrcz1wc3va981rp13rn4hyz-git-2.44.1-debug" + }, + { + "name": "doc", + "path": "/nix/store/rvgnmd4cwvmy3v77ig977ix40d2mipri-git-2.44.1-doc" + } + ], + "store_path": "/nix/store/jxjzgz1p0hdqsg8k17ivw5mgcd6ffx9c-git-2.44.1" + } + } + }, + "go-swag@1.8.4": { + "last_modified": "2022-08-27T08:52:24Z", + "resolved": "github:NixOS/nixpkgs/ed0fab06cc1ca9799e6dda30529c963b95c4dc2a#go-swag", + "source": "devbox-search", + "version": "1.8.4", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/hk5sff5gcdz3d5yvmr0nj7vhkypavxdc-go-swag-1.8.4", + "default": true + } + ], + "store_path": "/nix/store/hk5sff5gcdz3d5yvmr0nj7vhkypavxdc-go-swag-1.8.4" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/sm5i3rfpcd2ci7mdsc3gvc6rkcz3wc3d-go-swag-1.8.4", + "default": true + } + ], + "store_path": "/nix/store/sm5i3rfpcd2ci7mdsc3gvc6rkcz3wc3d-go-swag-1.8.4" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/jl619a945gj554dcfkg2c3jzfc1x3w6d-go-swag-1.8.4", + "default": true + } + ], + "store_path": "/nix/store/jl619a945gj554dcfkg2c3jzfc1x3w6d-go-swag-1.8.4" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/6663cjz2g1pwm2pbwgd5wb9w0z3kapbw-go-swag-1.8.4", + "default": true + } + ], + "store_path": "/nix/store/6663cjz2g1pwm2pbwgd5wb9w0z3kapbw-go-swag-1.8.4" + } + } + }, + "go@1.21": { + "last_modified": "2024-06-07T15:28:25Z", + "resolved": "github:NixOS/nixpkgs/d226935fd75012939397c83f6c385e4d6d832288#go_1_21", + "source": "devbox-search", + "version": "1.21.11", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/l12h01lkwgmfgck89h89ajny5jcdk5nd-go-1.21.11", + "default": true + } + ], + "store_path": "/nix/store/l12h01lkwgmfgck89h89ajny5jcdk5nd-go-1.21.11" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/hgdvcdyhbc1g10x6db92xi5qf0ysfpp7-go-1.21.11", + "default": true + } + ], + "store_path": "/nix/store/hgdvcdyhbc1g10x6db92xi5qf0ysfpp7-go-1.21.11" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/bnnawv8kk5xkjxzanklq52xd03cw2y5b-go-1.21.11", + "default": true + } + ], + "store_path": "/nix/store/bnnawv8kk5xkjxzanklq52xd03cw2y5b-go-1.21.11" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/dw0dxwi4w2g4kmmw1zcwrx9srn8yam2w-go-1.21.11", + "default": true + } + ], + "store_path": "/nix/store/dw0dxwi4w2g4kmmw1zcwrx9srn8yam2w-go-1.21.11" + } + } + }, + "qemu@latest": { + "last_modified": "2024-05-23T08:10:22Z", + "resolved": "github:NixOS/nixpkgs/3305b2b25e4ae4baee872346eae133cf6f611783#qemu", + "source": "devbox-search", + "version": "8.2.4", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/nxb1q7j29jkw91kb3l79mx484mk67aq1-qemu-8.2.4", + "default": true + } + ], + "store_path": "/nix/store/nxb1q7j29jkw91kb3l79mx484mk67aq1-qemu-8.2.4" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/fll2y6hyvh9dnxw8vvydwgjf1n78pvrm-qemu-8.2.4", + "default": true + }, + { + "name": "ga", + "path": "/nix/store/k4w39x9sgsl4hb0713q8z6qz2agy8mqx-qemu-8.2.4-ga" + } + ], + "store_path": "/nix/store/fll2y6hyvh9dnxw8vvydwgjf1n78pvrm-qemu-8.2.4" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/w28pkd6kjxgcwqj6qm2d1d8jki49f7zm-qemu-8.2.4", + "default": true + } + ], + "store_path": "/nix/store/w28pkd6kjxgcwqj6qm2d1d8jki49f7zm-qemu-8.2.4" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/ck1jf678yi3x1n4idmr4q1c0bdj2s5x5-qemu-8.2.4", + "default": true + }, + { + "name": "debug", + "path": "/nix/store/a0n21sa5awbb90ag5n0iy75kilfp8xmj-qemu-8.2.4-debug" + }, + { + "name": "ga", + "path": "/nix/store/qvdbwf2jad7bcgf2j42yinyhrpbs5467-qemu-8.2.4-ga" + } + ], + "store_path": "/nix/store/ck1jf678yi3x1n4idmr4q1c0bdj2s5x5-qemu-8.2.4" + } + } + }, + "vim@latest": { + "last_modified": "2024-05-22T06:18:38Z", + "resolved": "github:NixOS/nixpkgs/3f316d2a50699a78afe5e77ca486ad553169061e#vim", + "source": "devbox-search", + "version": "9.1.0377", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/lmx8m6kzyvipfb0s0jfpmwvrfaakvr16-vim-9.1.0377", + "default": true + }, + { + "name": "xxd", + "path": "/nix/store/plkqnw8bx49w9kapywwcqlvid3f24lpy-vim-9.1.0377-xxd", + "default": true + } + ], + "store_path": "/nix/store/lmx8m6kzyvipfb0s0jfpmwvrfaakvr16-vim-9.1.0377" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/dgi2x0gadhr1h2vzfhab3kj5jbr74g6x-vim-9.1.0377", + "default": true + }, + { + "name": "xxd", + "path": "/nix/store/zk521mqyrx0i3nxiwmhpdsz93ia8rb6s-vim-9.1.0377-xxd", + "default": true + } + ], + "store_path": "/nix/store/dgi2x0gadhr1h2vzfhab3kj5jbr74g6x-vim-9.1.0377" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/l3j2ypqx0mq6kk2s3pgxiybmkawpc7ii-vim-9.1.0377", + "default": true + }, + { + "name": "xxd", + "path": "/nix/store/qgd4kpvdj8cx8cqfwh09hysh1yrvmfvj-vim-9.1.0377-xxd", + "default": true + } + ], + "store_path": "/nix/store/l3j2ypqx0mq6kk2s3pgxiybmkawpc7ii-vim-9.1.0377" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/2126qjwg5cx9aw47bj9b427hazh96sh7-vim-9.1.0377", + "default": true + }, + { + "name": "xxd", + "path": "/nix/store/3g9nvidfl7phh7zh03ixymy5nfscianb-vim-9.1.0377-xxd", + "default": true + } + ], + "store_path": "/nix/store/2126qjwg5cx9aw47bj9b427hazh96sh7-vim-9.1.0377" + } + } + } + } +} diff --git a/migrations/seed/role_permissions.sql b/migrations/seed/role_permissions.sql index 860e2fd69..dc38ce3d7 100644 --- a/migrations/seed/role_permissions.sql +++ b/migrations/seed/role_permissions.sql @@ -26,7 +26,9 @@ INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, rol ('7c6b1f91-f22f-46fb-9c71-5868967d485b', NULL, '2023-06-20 16:35:12.475872', '2023-06-20 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'e5a515aa-6f4f-4ac8-8eac-490a9a358833'), -- deliveryMetrics.read ('ec847eff-7ca4-4b57-a406-96a44f09e0fd', NULL, '2022-11-11 18:35:27.069944', '2022-11-11 18:35:27.069944', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', '51a3ec4a-6bae-4f02-9d9b-89ba539346da'), -- deliveryMetrics.leaderBoard.read ('df6f9d87-a1be-42dd-be96-f085245bf039', NULL, '2023-07-26 16:35:12.475872', '2023-07-26 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'fea2497c-694d-43d7-82cd-764d622a6706'), -- deliveryMetrics.leaderBoard.sync -('fdded785-6cc8-41e9-9fe4-5130b40584f9', NULL, '2023-07-26 16:35:12.475872', '2023-07-26 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'b4edad19-3e30-4e86-bd6e-07f535011aaf'); -- employees.discord.read +('fdded785-6cc8-41e9-9fe4-5130b40584f9', NULL, '2023-07-26 16:35:12.475872', '2023-07-26 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'b4edad19-3e30-4e86-bd6e-07f535011aaf'), -- employees.discord.read +('ae57f72a-1b48-4fdf-a76d-a0c796dc0943', NULL, '2023-07-26 16:35:12.475872', '2023-07-26 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', '1641106e-9a94-42fa-80e0-9d6978cd3596'), -- employees.discord.edit +('81e3fd29-8bf4-408e-99db-42d27ca0b816', NULL, '2023-07-26 16:35:12.475872', '2023-07-26 16:35:12.475872', '3fcf9e36-2501-4f86-8418-cfe3a137b7f9', 'b069e35c-1144-4554-9854-ff529506d4e5'); -- employees.discord.create -- FORTRESS VALUATION INSERT INTO public.role_permissions (id, deleted_at, created_at, updated_at, role_id, permission_id) VALUES diff --git a/pkg/handler/discord/discord.go b/pkg/handler/discord/discord.go index ed42bd380..d61f8dc0e 100644 --- a/pkg/handler/discord/discord.go +++ b/pkg/handler/discord/discord.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "net/http" + "sort" "strings" "time" @@ -494,20 +495,24 @@ func (h *handler) ListScheduledEvent(c *gin.Context) { ) var err error - after := time.Now().UTC() - afterStr := c.Query("after") - if strings.TrimSpace(afterStr) != "" { - after, err = time.Parse(time.RFC3339, afterStr) - if err != nil { - l.Error(err, "failed to parse query after to datetime") - c.JSON(http.StatusBadRequest, view.CreateResponse[any](nil, nil, err, nil, "")) - return - } + // get scheduled events from discord + discordScheduledEvents, err := h.service.Discord.ListEvents() + if err != nil { + l.Error(err, "failed to get scheduled events") + c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, "")) + return } + eventIDs := make([]string, 0) + for _, e := range discordScheduledEvents { + eventIDs = append(eventIDs, e.ID) + } + + // Get future events + now := time.Now() events, err := h.store.DiscordEvent.All(h.repo.DB(), &discordevent.Query{ - After: &after, + DiscordEventIDs: eventIDs, }, false) if err != nil { l.Error(err, "failed to get events") @@ -515,6 +520,37 @@ func (h *handler) ListScheduledEvent(c *gin.Context) { return } + mapUpcomingEvents := make(map[string]bool) + for _, e := range events { + mapUpcomingEvents[e.DiscordEventID] = true + } + + // Get completed events in the last 7 days, sometimes we need to update these events + // If the event has date in the future, but cannot be found in discord, it means it has been done earlier + after := now.AddDate(0, 0, -7) + completedEvents, err := h.store.DiscordEvent.All(h.repo.DB(), &discordevent.Query{ + After: &after, + }, false) + if err != nil { + l.Error(err, "failed to get completed events") + c.JSON(http.StatusInternalServerError, view.CreateResponse[any](nil, nil, err, nil, "")) + return + } + + for i := range completedEvents { + event := completedEvents[i] + if _, ok := mapUpcomingEvents[event.DiscordEventID]; ok { + continue + } + + event.IsOver = true + events = append(events, event) + } + + sort.Slice(events, func(i, j int) bool { + return events[i].Date.After(events[j].Date) + }) + c.JSON(http.StatusOK, view.CreateResponse[any](events, nil, nil, nil, "ok")) } diff --git a/pkg/model/event.go b/pkg/model/event.go index 0407e84a1..6d108981e 100644 --- a/pkg/model/event.go +++ b/pkg/model/event.go @@ -16,6 +16,7 @@ type Event struct { DiscordCreatorID string `json:"discord_creator_id"` EventType DiscordScheduledEventType `json:"type"` EventSpeakers []EventSpeaker `json:"event_speakers"` + IsOver bool `json:"is_over" gorm:"-"` } // EventSpeaker struct diff --git a/pkg/store/discordevent/discord_event.go b/pkg/store/discordevent/discord_event.go index a2863d0fd..169eeb91e 100644 --- a/pkg/store/discordevent/discord_event.go +++ b/pkg/store/discordevent/discord_event.go @@ -39,6 +39,10 @@ func (s *store) All(db *gorm.DB, q *Query, preload bool) ([]*model.Event, error) query = query.Where("date > ?", q.After) } + if len(q.DiscordEventIDs) > 0 { + query = query.Where("discord_event_id IN (?)", q.DiscordEventIDs) + } + if !preload { return e, query.Find(&e).Error } diff --git a/pkg/store/discordevent/interface.go b/pkg/store/discordevent/interface.go index ec3f07c72..6d4c83a9c 100644 --- a/pkg/store/discordevent/interface.go +++ b/pkg/store/discordevent/interface.go @@ -17,10 +17,11 @@ type IStore interface { // Query present invoice query from user type Query struct { - ID string - DiscordEventID string - EventTypes []model.EventType - Limit int - Offset int - After *time.Time + ID string + DiscordEventID string // For querying by single ID + DiscordEventIDs []string // For filter by multiple IDs + EventTypes []model.EventType + Limit int + Offset int + After *time.Time }