Feature/add price to subscription (#1551)
* Add price * Update changelog
This commit is contained in:
parent
7c4c047140
commit
1ae8970045
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added the price to the `Subscription` database schema
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Changed the execution time of the asset profile data gathering to every Sunday at lunch time
|
- Changed the execution time of the asset profile data gathering to every Sunday at lunch time
|
||||||
|
@ -8,7 +8,6 @@ import { TagService } from '@ghostfolio/api/services/tag/tag.service';
|
|||||||
import {
|
import {
|
||||||
DEMO_USER_ID,
|
DEMO_USER_ID,
|
||||||
PROPERTY_IS_READ_ONLY_MODE,
|
PROPERTY_IS_READ_ONLY_MODE,
|
||||||
PROPERTY_IS_USER_SIGNUP_ENABLED,
|
|
||||||
PROPERTY_SLACK_COMMUNITY_USERS,
|
PROPERTY_SLACK_COMMUNITY_USERS,
|
||||||
PROPERTY_STRIPE_CONFIG,
|
PROPERTY_STRIPE_CONFIG,
|
||||||
PROPERTY_SYSTEM_MESSAGE,
|
PROPERTY_SYSTEM_MESSAGE,
|
||||||
@ -303,14 +302,14 @@ export class InfoService {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const stripeConfig = await this.prismaService.property.findUnique({
|
let subscriptions: Subscription[] = [];
|
||||||
|
|
||||||
|
const stripeConfig = (await this.prismaService.property.findUnique({
|
||||||
where: { key: PROPERTY_STRIPE_CONFIG }
|
where: { key: PROPERTY_STRIPE_CONFIG }
|
||||||
});
|
})) ?? { value: '{}' };
|
||||||
|
|
||||||
if (stripeConfig) {
|
subscriptions = [JSON.parse(stripeConfig.value)];
|
||||||
return [JSON.parse(stripeConfig.value)];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
return subscriptions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,7 @@ export class SubscriptionController {
|
|||||||
|
|
||||||
await this.subscriptionService.createSubscription({
|
await this.subscriptionService.createSubscription({
|
||||||
duration: coupon.duration,
|
duration: coupon.duration,
|
||||||
|
price: 0,
|
||||||
userId: this.request.user.id
|
userId: this.request.user.id
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
||||||
import { PrismaService } from '@ghostfolio/api/services/prisma.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 { SubscriptionType } from '@ghostfolio/common/types/subscription.type';
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { Subscription } from '@prisma/client';
|
import { Subscription } from '@prisma/client';
|
||||||
@ -70,13 +74,16 @@ export class SubscriptionService {
|
|||||||
|
|
||||||
public async createSubscription({
|
public async createSubscription({
|
||||||
duration = '1 year',
|
duration = '1 year',
|
||||||
|
price,
|
||||||
userId
|
userId
|
||||||
}: {
|
}: {
|
||||||
duration?: StringValue;
|
duration?: StringValue;
|
||||||
|
price: number;
|
||||||
userId: string;
|
userId: string;
|
||||||
}) {
|
}) {
|
||||||
await this.prismaService.subscription.create({
|
await this.prismaService.subscription.create({
|
||||||
data: {
|
data: {
|
||||||
|
price,
|
||||||
expiresAt: addMilliseconds(new Date(), ms(duration)),
|
expiresAt: addMilliseconds(new Date(), ms(duration)),
|
||||||
User: {
|
User: {
|
||||||
connect: {
|
connect: {
|
||||||
@ -93,7 +100,21 @@ export class SubscriptionService {
|
|||||||
aCheckoutSessionId
|
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, {
|
await this.stripe.customers.update(session.customer as string, {
|
||||||
description: session.client_reference_id
|
description: session.client_reference_id
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Subscription" ADD COLUMN "price" DOUBLE PRECISION;
|
@ -147,6 +147,7 @@ model Subscription {
|
|||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
expiresAt DateTime
|
expiresAt DateTime
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
price Float?
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
userId String
|
userId String
|
||||||
User User @relation(fields: [userId], references: [id])
|
User User @relation(fields: [userId], references: [id])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user