From 1ae8970045d24b2e673ad222a352e1bdce7f7528 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Wed, 28 Dec 2022 13:57:15 +0100 Subject: [PATCH] Feature/add price to subscription (#1551) * Add price * Update changelog --- CHANGELOG.md | 4 +++ apps/api/src/app/info/info.service.ts | 13 +++++----- .../subscription/subscription.controller.ts | 1 + .../app/subscription/subscription.service.ts | 25 +++++++++++++++++-- .../migration.sql | 2 ++ prisma/schema.prisma | 1 + 6 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 prisma/migrations/20221227203757_added_price_to_subscription/migration.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index 23432350f..205b87bd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- Added the price to the `Subscription` database schema + ### Changed - Changed the execution time of the asset profile data gathering to every Sunday at lunch time diff --git a/apps/api/src/app/info/info.service.ts b/apps/api/src/app/info/info.service.ts index f9eab7018..810611375 100644 --- a/apps/api/src/app/info/info.service.ts +++ b/apps/api/src/app/info/info.service.ts @@ -8,7 +8,6 @@ import { TagService } from '@ghostfolio/api/services/tag/tag.service'; import { DEMO_USER_ID, PROPERTY_IS_READ_ONLY_MODE, - PROPERTY_IS_USER_SIGNUP_ENABLED, PROPERTY_SLACK_COMMUNITY_USERS, PROPERTY_STRIPE_CONFIG, PROPERTY_SYSTEM_MESSAGE, @@ -303,14 +302,14 @@ export class InfoService { return undefined; } - const stripeConfig = await this.prismaService.property.findUnique({ + let subscriptions: Subscription[] = []; + + const stripeConfig = (await this.prismaService.property.findUnique({ where: { key: PROPERTY_STRIPE_CONFIG } - }); + })) ?? { value: '{}' }; - if (stripeConfig) { - return [JSON.parse(stripeConfig.value)]; - } + subscriptions = [JSON.parse(stripeConfig.value)]; - return []; + return subscriptions; } } diff --git a/apps/api/src/app/subscription/subscription.controller.ts b/apps/api/src/app/subscription/subscription.controller.ts index eee815652..4b21482ae 100644 --- a/apps/api/src/app/subscription/subscription.controller.ts +++ b/apps/api/src/app/subscription/subscription.controller.ts @@ -63,6 +63,7 @@ export class SubscriptionController { await this.subscriptionService.createSubscription({ duration: coupon.duration, + price: 0, userId: this.request.user.id }); diff --git a/apps/api/src/app/subscription/subscription.service.ts b/apps/api/src/app/subscription/subscription.service.ts index fa061f369..d3e9dcc7b 100644 --- a/apps/api/src/app/subscription/subscription.service.ts +++ b/apps/api/src/app/subscription/subscription.service.ts @@ -1,6 +1,10 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { PrismaService } from '@ghostfolio/api/services/prisma.service'; -import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; +import { + DEFAULT_LANGUAGE_CODE, + PROPERTY_STRIPE_CONFIG +} from '@ghostfolio/common/config'; +import { Subscription as SubscriptionInterface } from '@ghostfolio/common/interfaces/subscription.interface'; import { SubscriptionType } from '@ghostfolio/common/types/subscription.type'; import { Injectable, Logger } from '@nestjs/common'; import { Subscription } from '@prisma/client'; @@ -70,13 +74,16 @@ export class SubscriptionService { public async createSubscription({ duration = '1 year', + price, userId }: { duration?: StringValue; + price: number; userId: string; }) { await this.prismaService.subscription.create({ data: { + price, expiresAt: addMilliseconds(new Date(), ms(duration)), User: { connect: { @@ -93,7 +100,21 @@ export class SubscriptionService { aCheckoutSessionId ); - await this.createSubscription({ userId: session.client_reference_id }); + let subscriptions: SubscriptionInterface[] = []; + + const stripeConfig = (await this.prismaService.property.findUnique({ + where: { key: PROPERTY_STRIPE_CONFIG } + })) ?? { value: '{}' }; + + subscriptions = [JSON.parse(stripeConfig.value)]; + + const coupon = subscriptions[0]?.coupon ?? 0; + const price = subscriptions[0]?.price ?? 0; + + await this.createSubscription({ + price: price - coupon, + userId: session.client_reference_id + }); await this.stripe.customers.update(session.customer as string, { description: session.client_reference_id diff --git a/prisma/migrations/20221227203757_added_price_to_subscription/migration.sql b/prisma/migrations/20221227203757_added_price_to_subscription/migration.sql new file mode 100644 index 000000000..cf3f84ee9 --- /dev/null +++ b/prisma/migrations/20221227203757_added_price_to_subscription/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Subscription" ADD COLUMN "price" DOUBLE PRECISION; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b90a78040..3c252395e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -147,6 +147,7 @@ model Subscription { createdAt DateTime @default(now()) expiresAt DateTime id String @id @default(uuid()) + price Float? updatedAt DateTime @updatedAt userId String User User @relation(fields: [userId], references: [id])