Skip to content

Commit

Permalink
feat: refunder
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Feb 29, 2024
1 parent 1e052c4 commit c434fdc
Show file tree
Hide file tree
Showing 21 changed files with 1,166 additions and 70 deletions.
4 changes: 2 additions & 2 deletions config/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ service: helium
port: 11000

# "local" or "cluster"
type: local
type: cluster

# fast mode
# 1. does not start the cluster, automatically assumes that your cluster already created
# 2. does not clean up resources. This makes your future starts faster
fast: true
fast: false

# whether to start local cluster. If false, will connect to current context
startCluster: true
Expand Down
12 changes: 12 additions & 0 deletions config/tilt/dev.Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ def start(landscape, platform, service, port, live):
]

)

refunder_image_name = platform + "-" + service + "-refunder"
docker_build(
refunder_image_name,
'.',
dockerfile = './infra/dev.Dockerfile',
entrypoint='bun run ./src/index.ts refunder',
live_update=[
sync('.', '/app'),
]

)
8 changes: 8 additions & 0 deletions config/tilt/prod.Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def start(landscape, platform, service, port, live):
dockerfile = './infra/Dockerfile',
)

# build API image
refunder_image_name = platform + "-" + service + "-refunder"
docker_build(
refunder_image_name,
'.',
dockerfile = './infra/Dockerfile',
)

# Add Link
k8s_resource(
workload = api_image_name,
Expand Down
2 changes: 1 addition & 1 deletion infra/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM oven/bun:1.0.18 as builder
FROM oven/bun:1.0.27 as builder
WORKDIR /app
COPY package.json .
COPY bun.lockb .
Expand Down
2 changes: 1 addition & 1 deletion infra/dev.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM oven/bun:1.0.18
FROM oven/bun:1.0.27
WORKDIR /app
COPY package.json .
COPY bun.lockb .
Expand Down
2 changes: 1 addition & 1 deletion infra/k3d.lapras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
servers: 1
agents: 0
network: lapras
image: rancher/k3s:v1.27.9-k3s1
image: rancher/k3s:v1.29.2-rc3-k3s1
ports:
- port: 20010:80
nodeFilters:
Expand Down
9 changes: 6 additions & 3 deletions infra/root_chart/Chart.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ dependencies:
version: 1.4.0
- name: root-chart
repository: oci://ghcr.io/atomicloud/nitroso.zinc
version: 1.17.2
version: 1.19.1
- name: bun-cron-chart
repository: file://../cron_chart
version: 0.1.0
digest: sha256:3ff9c590c293fa5bc7ddf3ff8ddb0774c68d5c719b89fca890e63ea153355d67
generated: "2024-02-26T12:43:34.276733+08:00"
- name: bun-cron-chart
repository: file://../cron_chart
version: 0.1.0
digest: sha256:48b2f958b0d41e224bd3fb5188ad461d83d972e05d47bbd48d7ab72e7cf681fe
generated: "2024-02-29T12:31:01.139433+08:00"
7 changes: 6 additions & 1 deletion infra/root_chart/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
alias: bromine
repository: oci://ghcr.io/atomicloud/sulfoxide.bromine
- name: root-chart
version: 1.17.2
version: 1.19.1
condition: zinc.enable
alias: zinc
repository: oci://ghcr.io/atomicloud/nitroso.zinc
Expand All @@ -22,3 +22,8 @@ dependencies:
repository: file://../cron_chart
alias: scheduler
condition: scheduler.enabled
- name: bun-cron-chart
version: 0.1.0
repository: file://../cron_chart
alias: refunder
condition: refunder.enabled
17 changes: 16 additions & 1 deletion infra/root_chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Root Chart to a single Service
| Repository | Name | Version |
|------------|------|---------|
| file://../cron_chart | scheduler(bun-cron-chart) | 0.1.0 |
| oci://ghcr.io/atomicloud/nitroso.zinc | zinc(root-chart) | 1.17.2 |
| file://../cron_chart | refunder(bun-cron-chart) | 0.1.0 |
| oci://ghcr.io/atomicloud/nitroso.zinc | zinc(root-chart) | 1.19.1 |
| oci://ghcr.io/atomicloud/sulfoxide.bromine | bromine(sulfoxide-bromine) | 1.4.0 |

## Values
Expand All @@ -23,6 +24,20 @@ Root Chart to a single Service
| bromine.rootSecret.ref | string | `"NITROSO_HELIUM"` | DOPPLER Token Reference |
| bromine.storeName | string | `"nitroso-helium"` | Store name to create |
| bromine.target | string | `"nitroso-helium"` | |
| refunder.command[0] | string | `"bun"` | |
| refunder.command[1] | string | `"run"` | |
| refunder.command[2] | string | `"index.js"` | |
| refunder.command[3] | string | `"refunder"` | |
| refunder.envFromSecret | string | `"nitroso-helium"` | |
| refunder.restartPolicy | string | `"Never"` | |
| refunder.schedule | string | `"30 5-23 * * *"` | |
| refunder.serviceTree.<<.landscape | string | `"lapras"` | |
| refunder.serviceTree.<<.layer | string | `"2"` | |
| refunder.serviceTree.<<.module | string | `"pollee"` | |
| refunder.serviceTree.<<.platform | string | `"nitroso"` | |
| refunder.serviceTree.<<.service | string | `"helium"` | |
| refunder.serviceTree.module | string | `"refunder"` | |
| refunder.timeZone | string | `"Asia/Singapore"` | |
| scheduler.command[0] | string | `"bun"` | |
| scheduler.command[1] | string | `"run"` | |
| scheduler.command[2] | string | `"index.js"` | |
Expand Down
14 changes: 14 additions & 0 deletions infra/root_chart/values.lapras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ zinc:
tls: []
migration:
enabled: false
streamcache:
enable: true
fullnameOverride: "zinc-streamcache"
maincache:
enable: true
fullnameOverride: "zinc-maincache"
Expand All @@ -56,5 +59,16 @@ zinc:
scheduler:
schedule: "02 12 * * *"
fullnameOverride: "helium-scheduler"
image:
repository: nitroso-helium-scheduler
serviceTree:
landscape: *landscape

refunder:
schedule: "0 10-23 * * *"
timezone: "Asia/Singapore"
fullnameOverride: "helium-refunder"
image:
repository: nitroso-helium-refunder
serviceTree:
landscape: *landscape
17 changes: 17 additions & 0 deletions infra/root_chart/values.pichu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ scheduler:
limits:
memory: "512Mi"
cpu: "250m"

refunder:
enable: true
image:
repository: ghcr.io/atomicloud/nitroso.helium/nitroso-helium
serviceTree:
landscape: *landscape
securityContext: *securityContext
podSecurityContext: *podSecurityContext
fullnameOverride: "helium-refunder"
resources:
requests:
memory: "5Mi"
cpu: "5m"
limits:
memory: "128Mi"
cpu: "125m"
17 changes: 17 additions & 0 deletions infra/root_chart/values.pikachu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ scheduler:
limits:
memory: "512Mi"
cpu: "250m"

refunder:
enable: true
image:
repository: ghcr.io/atomicloud/nitroso.helium/nitroso-helium
serviceTree:
landscape: *landscape
securityContext: *securityContext
podSecurityContext: *podSecurityContext
fullnameOverride: "helium-refunder"
resources:
requests:
memory: "5Mi"
cpu: "5m"
limits:
memory: "128Mi"
cpu: "125m"
17 changes: 17 additions & 0 deletions infra/root_chart/values.raichu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ scheduler:
limits:
memory: "512Mi"
cpu: "250m"

refunder:
enable: true
image:
repository: ghcr.io/atomicloud/nitroso.helium/nitroso-helium
serviceTree:
landscape: *landscape
securityContext: *securityContext
podSecurityContext: *podSecurityContext
fullnameOverride: "helium-refunder"
resources:
requests:
memory: "5Mi"
cpu: "5m"
limits:
memory: "128Mi"
cpu: "125m"
6 changes: 6 additions & 0 deletions infra/root_chart/values.tauros.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ scheduler:
fullnameOverride: "helium-scheduler"
serviceTree:
landscape: *landscape

refunder:
enable: true
fullnameOverride: "helium-refunder"
serviceTree:
landscape: *landscape
17 changes: 17 additions & 0 deletions infra/root_chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ scheduler:
restartPolicy: "Never"

envFromSecret: *target

refunder:
serviceTree:
<<: *serviceTree
module: refunder

schedule: "30 5-23 * * *"
timeZone: "Asia/Singapore"
command:
- "bun"
- "run"
- "index.js"
- "refunder"

restartPolicy: "Never"

envFromSecret: *target
10 changes: 10 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Watcher } from "../lib/watcher.ts";
import { Get } from "../lib/get.ts";
import { AsciiTable3 } from "ascii-table3";
import { Updater } from "../lib/updater.ts";
import { Refunder } from "../lib/refunder.ts";

class Cli {
constructor(
Expand All @@ -16,6 +17,7 @@ class Cli {
private readonly watcher: Watcher,
private readonly getter: Get,
private readonly updater: Updater,
private readonly refunder: Refunder,
) {}

err(message: string): never {
Expand Down Expand Up @@ -57,6 +59,14 @@ class Cli {
}
});

program
.command("refunder")
.description("Initiate Refund Process")
.action(async () => {
await this.refunder.Refund();
process.exit(0);
});

program
.command("get <date> <from>")
.description("Get schedule for a fixed day and direction")
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Utility } from "./utility.ts";
import { Checker } from "./lib/checker.ts";
import { Updater } from "./lib/updater.ts";
import { Populator } from "./lib/populator.ts";
import { Refunder } from "./lib/refunder.ts";

// start up that cannot use DI
const landscape = process.env.LANDSCAPE;
Expand Down Expand Up @@ -62,6 +63,8 @@ const updater = new Updater(
populator,
);

const cli = new Cli(logger, cfg, zincDate, watcher, getter, updater);
const refunder = new Refunder(logger, zinc, utility);

const cli = new Cli(logger, cfg, zincDate, watcher, getter, updater, refunder);

await cli.start();
63 changes: 63 additions & 0 deletions src/lib/refunder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Logger } from "pino";
import { Api } from "./zinc/Api.ts";
import { Utility } from "../utility.ts";
import { Res } from "./core/result.ts";
import { BookingPrincipalRes } from "./zinc/data-contracts.ts";
import { ProblemDetails } from "../errors/problem_details.ts";
import { AggregatedError } from "../errors/v1/aggregate_error.ts";

class Refunder {
constructor(
private readonly logger: Logger,
private readonly zinc: Api,
private readonly utility: Utility,
) {}

async Refund(): Promise<void> {
this.logger.info("Starting refunding");

await this.utility
.toResult(
() => this.zinc.vBookingRefundDetail("1.0"),
"Failed to retrieve list of tickets to be refunded",
)
.map(async (tickets) => {
const res: (["err", ProblemDetails] | ["ok", BookingPrincipalRes])[] =
[];

for (const ticket of tickets) {
const r = await this.utility
.toResult(async () => {
this.logger.info({ ticket: ticket.id }, "Refunding ticket");
return await this.zinc.vBookingRefundCreate(ticket.id, "1.0");
}, `Failed to refund ticket ${ticket.id}`)
.serial();
res.push(r);
}

const results = Res.all(...res.map((x) => Res.fromSerial(x)));

return results.mapErr(
(x) =>
({
data: new AggregatedError(
"Failed to refund tickets",
x.map((y) => y),
),
detail: "Failed to some refund tickets, see sub-problems",
type: "aggregate_error",
status: 400,
title: "Failed to refund tickets",
traceId: "see sub-problems",
}) satisfies ProblemDetails,
);
})
.match({
err: (e) => this.logger.error({ error: e }, "Failed to refund tickets"),
ok: (ok) => this.logger.info({ result: ok }, "Completed refunding"),
});
this.logger.info("Completed refunding");
}
}

export { Refunder };
Loading

0 comments on commit c434fdc

Please sign in to comment.