Skip to content

Commit

Permalink
Merge branch 'release/v0.0.99'
Browse files Browse the repository at this point in the history
* release/v0.0.99:
  feat: cronjob sync icy tx
  feat: add migration for icy txn and reports views
  • Loading branch information
namnhce committed May 15, 2023
2 parents 83904f8 + 434b16c commit 6a09fc3
Show file tree
Hide file tree
Showing 21 changed files with 655 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,47 @@ const docTemplate = `{
}
}
},
"/cron-jobs/store-vault-transaction": {
"post": {
"description": "Store vault tx as icy tx from Mochi service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Vault"
],
"summary": "Store vault tx as icy tx from Mochi service",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/view.MessageResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
}
}
}
},
"/cron-jobs/sync-project-member-status": {
"put": {
"description": "Sync project member status",
Expand Down
41 changes: 41 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,47 @@
}
}
},
"/cron-jobs/store-vault-transaction": {
"post": {
"description": "Store vault tx as icy tx from Mochi service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Vault"
],
"summary": "Store vault tx as icy tx from Mochi service",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/view.MessageResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"404": {
"description": "Not Found",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
"schema": {
"$ref": "#/definitions/view.ErrorResponse"
}
}
}
}
},
"/cron-jobs/sync-project-member-status": {
"put": {
"description": "Sync project member status",
Expand Down
27 changes: 27 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4526,6 +4526,33 @@ paths:
summary: Update client by id
tags:
- Client
/cron-jobs/store-vault-transaction:
post:
consumes:
- application/json
description: Store vault tx as icy tx from Mochi service
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/view.MessageResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/view.ErrorResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/view.ErrorResponse'
"500":
description: Internal Server Error
schema:
$ref: '#/definitions/view.ErrorResponse'
summary: Store vault tx as icy tx from Mochi service
tags:
- Vault
/cron-jobs/sync-project-member-status:
put:
consumes:
Expand Down
72 changes: 72 additions & 0 deletions migrations/schemas/20230512091353-drop-old-icy-txn.sql
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;
27 changes: 27 additions & 0 deletions migrations/schemas/20230512091526-new-icy-txn.sql
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";
143 changes: 143 additions & 0 deletions migrations/schemas/20230512110852-report-views.sql
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";
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;
Loading

0 comments on commit 6a09fc3

Please sign in to comment.