-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* release/v0.0.99: feat: cronjob sync icy tx feat: add migration for icy txn and reports views
- Loading branch information
Showing
21 changed files
with
655 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
-- +migrate Up | ||
|
||
DROP VIEW "vw_icy_treasury_funds"; | ||
|
||
DROP TABLE "icy_transactions_out"; | ||
|
||
DROP TABLE "icy_transactions_in"; | ||
|
||
DROP TABLE "icy_treasury_categories"; | ||
|
||
-- +migrate Down | ||
|
||
CREATE TABLE "icy_treasury_categories" ( | ||
"id" uuid NOT NULL DEFAULT uuid(), | ||
"created_at" timestamp(8) DEFAULT NOW(), | ||
"deleted_at" timestamp(8), | ||
"name" text, | ||
"category_manager_id" uuid, | ||
CONSTRAINT "icy_treasury_categories_category_manager_id_fkey" FOREIGN KEY ("category_manager_id") REFERENCES "employees" ("id"), | ||
PRIMARY KEY ("id") | ||
); | ||
|
||
CREATE TABLE "icy_transactions_out" ( | ||
"id" uuid NOT NULL DEFAULT uuid(), | ||
"created_at" timestamp(8) DEFAULT NOW(), | ||
"deleted_at" timestamp(8), | ||
"amount" text, | ||
"description" text, | ||
"category_id" uuid, | ||
"to_employee_id" uuid NOT NULL, | ||
"approver_id" uuid, | ||
CONSTRAINT "icy_transactions_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "icy_treasury_categories" ("id"), | ||
CONSTRAINT "icy_transactions_to_employee_id_fkey" FOREIGN KEY ("to_employee_id") REFERENCES "employees" ("id"), | ||
CONSTRAINT "icy_transactions_approver_id_fkey" FOREIGN KEY ("approver_id") REFERENCES "employees" ("id"), | ||
PRIMARY KEY ("id") | ||
); | ||
|
||
CREATE TABLE "icy_transactions_in" ( | ||
"id" uuid NOT NULL DEFAULT uuid(), | ||
"created_at" timestamp(8) DEFAULT NOW(), | ||
"deleted_at" timestamp(8), | ||
"date" timestamp(8) DEFAULT NOW(), | ||
"description" text, | ||
"amount" text, | ||
"category_id" uuid, | ||
CONSTRAINT "icy_transactions_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "icy_treasury_categories" ("id"), | ||
PRIMARY KEY ("id") | ||
); | ||
|
||
CREATE VIEW "vw_icy_treasury_funds" AS | ||
SELECT | ||
t1.category_id, | ||
t2.total_in - t1.total_out AS balance | ||
FROM | ||
( | ||
SELECT | ||
category_id, | ||
SUM(amount :: NUMERIC) AS total_out | ||
FROM | ||
icy_transactions_out ito | ||
GROUP BY | ||
category_id | ||
) t1 | ||
JOIN ( | ||
SELECT | ||
category_id, | ||
SUM(amount :: NUMERIC) AS total_in | ||
FROM | ||
icy_transactions_in iti | ||
GROUP BY | ||
category_id | ||
) t2 ON t1.category_id = t2.category_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- +migrate Up | ||
|
||
CREATE TYPE "enum_icy_txn_category" AS ENUM ('learning', 'community', 'delivery', 'tooling'); | ||
|
||
CREATE TABLE IF NOT EXISTS "icy_transactions" ( | ||
"id" uuid PRIMARY KEY DEFAULT uuid(), | ||
"created_at" timestamptz NOT NULL DEFAULT now(), | ||
"updated_at" timestamptz NOT NULL DEFAULT now(), | ||
"deleted_at" timestamptz, | ||
"txn_time" timestamptz NOT NULL DEFAULT now(), | ||
"src_employee_id" uuid REFERENCES employees ("id"), | ||
"dest_employee_id" uuid REFERENCES employees ("id"), | ||
"category" enum_icy_txn_category NOT NULL, | ||
"amount" numeric NOT NULL DEFAULT 0, | ||
"note" text | ||
); | ||
|
||
ALTER TABLE | ||
audiences | ||
ADD | ||
column unsub_at TIMESTAMP default null; | ||
|
||
-- +migrate Down | ||
|
||
DROP TABLE icy_transactions; | ||
|
||
DROP TYPE IF EXISTS "enum_team"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
-- +migrate Up | ||
|
||
CREATE VIEW "vw_idle_employees" AS | ||
SELECT | ||
DISTINCT emp.* | ||
FROM | ||
employees emp | ||
JOIN project_members pj ON pj.employee_id = emp.id | ||
AND emp.working_status <> 'left' | ||
WHERE | ||
deployment_type <> 'shadow' | ||
OR rate = 0; | ||
|
||
CREATE VIEW "vw_subscribers_last_7days" AS | ||
SELECT | ||
* | ||
FROM | ||
audiences | ||
WHERE now() :: date - created_at :: date <= 7; | ||
|
||
CREATE OR REPLACE VIEW "vw_icy_earning_by_team_monthly" AS | ||
WITH weekly_earning AS ( | ||
SELECT | ||
date_trunc('month', txn_time) AS "month", | ||
date_trunc('week', txn_time) AS "week", | ||
category, | ||
SUM(amount) AS "amount" | ||
FROM | ||
icy_transactions | ||
GROUP BY | ||
date_trunc('month', txn_time), | ||
date_trunc('week', txn_time), | ||
category | ||
ORDER BY | ||
date_trunc('week', txn_time) DESC, | ||
SUM(amount) DESC | ||
) | ||
SELECT | ||
to_char(date_trunc('month', txn_time), 'yyyy-mm') as "period", | ||
t.category as "team", | ||
SUM(t.amount) as "amount", | ||
AVG(w.amount) AS "avg_earning_weekly" | ||
FROM | ||
icy_transactions t | ||
LEFT JOIN weekly_earning w ON w.category = t.category | ||
AND w.month = date_trunc('month', txn_time) | ||
GROUP BY | ||
date_trunc('month', txn_time), | ||
t.category | ||
ORDER BY | ||
date_trunc('month', txn_time) DESC, | ||
SUM(t.amount) DESC; | ||
|
||
CREATE OR REPLACE VIEW "vw_icy_earning_by_team_all_time" AS WITH monthly_earning AS ( | ||
SELECT | ||
date_trunc('month', txn_time) AS "month", | ||
category, | ||
SUM(amount) AS "amount" | ||
FROM | ||
icy_transactions | ||
GROUP BY | ||
date_trunc('month', txn_time), | ||
category | ||
ORDER BY | ||
date_trunc('month', txn_time) DESC, | ||
SUM(amount) DESC | ||
), | ||
weekly_earning AS ( | ||
SELECT | ||
date_trunc('month', txn_time) AS "month", | ||
date_trunc('week', txn_time) AS "week", | ||
category, | ||
SUM(amount) AS "amount" | ||
FROM | ||
icy_transactions | ||
GROUP BY | ||
date_trunc('month', txn_time), | ||
date_trunc('week', txn_time), | ||
category | ||
ORDER BY | ||
date_trunc('week', txn_time) DESC, | ||
SUM(amount) DESC | ||
) | ||
SELECT | ||
m.category AS "team", | ||
SUM(t.amount) AS "amount", | ||
AVG(m.amount) AS "avg_earning_monthy", | ||
AVG(w.amount) AS "avg_earning_weekly" | ||
FROM | ||
icy_transactions t | ||
LEFT JOIN monthly_earning m ON m.category = t.category | ||
LEFT JOIN weekly_earning w ON w.category = t.category | ||
AND w.month = m.month | ||
GROUP BY | ||
m.category | ||
ORDER BY | ||
SUM(t.amount) DESC; | ||
|
||
CREATE OR REPLACE VIEW "vw_icy_earning_by_team_weekly" AS | ||
SELECT | ||
to_char(date_trunc('week', txn_time), 'yyyy-mm-dd') AS "period", | ||
category AS "team", | ||
SUM(t.amount) AS "amount" | ||
FROM | ||
icy_transactions t | ||
GROUP BY | ||
date_trunc('week', txn_time), | ||
category | ||
ORDER BY | ||
date_trunc('week', txn_time) DESC, | ||
SUM(t.amount) DESC; | ||
|
||
CREATE OR REPLACE VIEW "vw_icy_employee_dashboard" AS | ||
SELECT | ||
t.dest_employee_id as employee_id, | ||
e.full_name, | ||
e.team_email, | ||
e.personal_email, | ||
SUM(t.amount) AS "total_earned" | ||
FROM | ||
icy_transactions t | ||
LEFT JOIN employees e ON e.id = t.dest_employee_id | ||
GROUP BY | ||
t.dest_employee_id, | ||
e.full_name, | ||
e.team_email, | ||
e.personal_email | ||
ORDER BY | ||
SUM(t.amount) DESC; | ||
|
||
-- +migrate Down | ||
|
||
DROP VIEW IF EXISTS "vw_subscribers_last_7days"; | ||
|
||
DROP VIEW IF EXISTS "vw_icy_earning_by_team_weekly"; | ||
|
||
DROP VIEW IF EXISTS "vw_icy_earning_by_team_monthly"; | ||
|
||
DROP VIEW IF EXISTS "vw_icy_earning_by_team_all_time"; | ||
|
||
DROP VIEW IF EXISTS "vw_icy_employee_dashboard"; | ||
|
||
DROP VIEW IF EXISTS "vw_idle_employees"; |
15 changes: 15 additions & 0 deletions
15
migrations/schemas/20230513041758-create_icy_transactions_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- +migrate Up | ||
CREATE TABLE IF NOT EXISTS icy_transactions ( | ||
id uuid PRIMARY KEY DEFAULT (uuid()), | ||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
deleted_at TIMESTAMPTZ, | ||
vault TEXT NOT NULL, | ||
amount TEXT NOT NULL, | ||
token TEXT NOT NULL, | ||
sender_discord_id TEXT NOT NULL, | ||
recipient_address TEXT NOT NULL, | ||
recipient_discord_id TEXT NOT NULL | ||
); | ||
-- +migrate Down | ||
DROP TABLE IF EXISTS icy_transactions; |
Oops, something went wrong.