Skip to content

Commit

Permalink
Merge pull request #10 from beabee-communityrm/build/1095-core-db-mig…
Browse files Browse the repository at this point in the history
…rations

build: move database migrations to core
  • Loading branch information
wpf500 authored Jul 8, 2024
2 parents 705a4ce + 53d3e80 commit a8147ba
Show file tree
Hide file tree
Showing 104 changed files with 46 additions and 20 deletions.
3 changes: 2 additions & 1 deletion apps/backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ x-base-app: &base-app
volumes:
- ./built:/opt/apps/backend/built
- ../../packages/common/dist:/opt/packages/common/dist
- ./src/migrations:/opt/apps/backend/src/migrations
- ../../packages/core/dist:/opt/packages/core/dist
- ../../packages/core/src/migrations:/opt/packages/core/src/migrations
env_file:
- .env
environment:
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"check:prettier": "prettier -c .",
"check:dependencies": "dpdm -T --no-warning --no-tree --exit-code circular:1 src/app.ts src/webhooks/app.ts src/api/app.ts",
"format": "prettier --write .",
"typeorm": "typeorm -d ./built/core/database.js",
"typeorm": "typeorm -d ./built/core/lib/typeorm.js",
"test": "jest --setupFiles dotenv/config",
"docker:build": "docker compose build",
"docker:start": "docker compose up -d",
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/core/lib/typeorm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* This is used by the TypeORM CLI to run migrations
*/
export { dataSource } from "@beabee/core/database";
3 changes: 1 addition & 2 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import config from "./config/config";

const log = mainLogger.child({ app: "database" });

// This is used by the TypeORM CLI to run migrations
export const dataSource: DataSource = new DataSource({
type: "postgres",
url: config.databaseUrl,
logging: config.dev,
entities: [__dirname + "/models/*.js"],
// TODO: migrations: [__dirname + "/../migrations/*.js"]
migrations: [__dirname + "/migrations/*.js"]
});

export function runTransaction<T>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MigrationInterface, QueryRunner } from "typeorm";

import { addThenSetNotNull } from "@core/utils/db";
import { addThenSetNotNull } from "../utils/db";

export class AddEmailAndPasswordToJoinFlow1621513644609
implements MigrationInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { addThenSetNotNull } from "@core/utils/db";
import { addThenSetNotNull } from "../utils/db";

export class AddButtonText1632397910432 implements MigrationInterface {
name = "AddButtonText1632397910432";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addThenSetNotNull } from "@core/utils/db";
import { MigrationInterface, QueryRunner } from "typeorm";
import { addThenSetNotNull } from "../utils/db";

export class AddExcerptAndImageToPoll1634125137636
implements MigrationInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addThenSetNotNull } from "@core/utils/db";
import { MigrationInterface, QueryRunner } from "typeorm";
import { addThenSetNotNull } from "../utils/db";

export class AddPaymentMethodToJoinForm1650900384747
implements MigrationInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { addThenSetNotNull } from "@core/utils/db";
import { MigrationInterface, QueryRunner } from "typeorm";
import { addThenSetNotNull } from "../utils/db";

export class AddUrlsToJoinFlow1652115873327 implements MigrationInterface {
name = "AddUrlsToJoinFlow1652115873327";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContributionPeriod, PaymentMethod } from "@beabee/beabee-common";
import { MigrationInterface, QueryRunner } from "typeorm";

import { getChargeableAmount } from "@core/utils/payment";
import { getChargeableAmount } from "../utils/payment";

interface PaymentQueryResults {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/models/Contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
PrimaryGeneratedColumn
} from "typeorm";

import { getActualAmount } from "../utils";
import { getActualAmount } from "../utils/payment";
import config from "../config/config";

import type ContactContribution from "./ContactContribution";
Expand Down
File renamed without changes.
9 changes: 0 additions & 9 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ContributionPeriod } from "@beabee/beabee-common";
import { RequestHandler } from "express";

export function wrapAsync(fn: RequestHandler): RequestHandler {
Expand All @@ -11,14 +10,6 @@ export function wrapAsync(fn: RequestHandler): RequestHandler {
};
}

export function getActualAmount(
amount: number,
period: ContributionPeriod
): number {
// TODO: fix this properly
return Math.round(amount * (period === ContributionPeriod.Annually ? 12 : 1));
}

export function extractToken(authHeader?: string): string | null {
if (!authHeader) return null;
if (authHeader?.startsWith("Bearer ")) {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/utils/payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { calcPaymentFee, ContributionPeriod, PaymentForm, PaymentMethod } from "@beabee/beabee-common";
import config from "../config/config";

export function getActualAmount(
amount: number,
period: ContributionPeriod
): number {
// TODO: fix this properly
return Math.round(amount * (period === ContributionPeriod.Annually ? 12 : 1));
}

/**
* Calculate the amount to charge including the payment fee if applicable
*
* @param paymentForm The payment form
* @param paymentMethod The payment method
* @returns The chargeable amount in cents
*/
export function getChargeableAmount(
paymentForm: PaymentForm,
paymentMethod: PaymentMethod
): number {
const amount = getActualAmount(paymentForm.monthlyAmount, paymentForm.period);
const fee = paymentForm.payFee
? calcPaymentFee(
{ amount, period: paymentForm.period, paymentMethod },
config.stripe.country
)
: 0;
return Math.round((amount + fee) * 100);
}

0 comments on commit a8147ba

Please sign in to comment.