Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): add support for automatic subscription enroll on aws marketplace integration #8698

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/amplication-server/src/core/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { OpenIDConnectAuthMiddleware } from "./oidc.middleware";
import { SegmentAnalyticsModule } from "../../services/segmentAnalytics/segmentAnalytics.module";
import { IdpModule } from "../idp/idp.module";
import { PreviewUserService } from "./previewUser.service";
import { AwsMarketplaceModule } from "../aws-marketplace/aws-marketplace.module";

@Module({
imports: [
Expand All @@ -49,6 +50,7 @@ import { PreviewUserService } from "./previewUser.service";
forwardRef(() => WorkspaceModule),
forwardRef(() => UserModule),
IdpModule,
AwsMarketplaceModule,
],
providers: [
AuthService,
Expand Down
10 changes: 9 additions & 1 deletion packages/amplication-server/src/core/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { SignupWithBusinessEmailArgs } from "./dto/SignupWithBusinessEmailArgs";
import { AuthProfile, AuthUser } from "./types";
import { PreviewUserService } from "./previewUser.service";
import { Auth0User } from "../idp/types";
import { AwsMarketplaceService } from "../aws-marketplace/aws-marketplace.service";

const TOKEN_PREVIEW_LENGTH = 8;
const TOKEN_EXPIRY_DAYS = 30;
Expand Down Expand Up @@ -72,7 +73,8 @@ export class AuthService {
private readonly analytics: SegmentAnalyticsService,
private readonly auth0Service: Auth0Service,
@Inject(forwardRef(() => PreviewUserService))
private readonly previewUserService: PreviewUserService
private readonly previewUserService: PreviewUserService,
private readonly awsMarketplaceService: AwsMarketplaceService
) {
this.clientHost = configService.get(Env.CLIENT_HOST);
}
Expand Down Expand Up @@ -578,6 +580,12 @@ export class AuthService {
}
}

await this.awsMarketplaceService.completeAwsMarketplaceIntegration(
profile.email,
user.account.id,
user.workspace.id
);

this.trackCompleteEmailSignup(user.account, profile, existingUser);

await this.configureJtw(response, user, isNew, isFromPreviewUser);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Module } from "@nestjs/common";
import { Module, forwardRef } from "@nestjs/common";
import { PrismaModule } from "../../prisma/prisma.module";
import { AwsMarketplaceController } from "./aws-marketplace.controller";
import { AuthModule } from "../auth/auth.module";
import { AwsMarketplaceService } from "./aws-marketplace.service";
import { BillingModule } from "../billing/billing.module";

@Module({
imports: [AuthModule, PrismaModule],
imports: [forwardRef(() => AuthModule), BillingModule, PrismaModule],
providers: [AwsMarketplaceService],
controllers: [AwsMarketplaceController],
exports: [],
exports: [AwsMarketplaceService],
})
export class AwsMarketplaceModule {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable } from "@nestjs/common";
import { Inject, Injectable, forwardRef } from "@nestjs/common";
import {
MarketplaceMeteringClient,
ResolveCustomerCommand,
Expand All @@ -20,6 +20,9 @@ import * as cookie from "cookie";
import { Env } from "../../env";
import { PrismaService } from "../../prisma";
import { AuthService } from "../auth/auth.service";
import { BillingService } from "../billing/billing.service";
import { BillingPlan } from "@amplication/util-billing-types";
import { BillingPeriod } from "@stigg/node-server-sdk";

@Injectable()
export class AwsMarketplaceService {
Expand All @@ -33,7 +36,9 @@ export class AwsMarketplaceService {
@Inject(AmplicationLogger) private readonly logger: AmplicationLogger,
configService: ConfigService,
private readonly prismaService: PrismaService,
private readonly authService: AuthService
@Inject(forwardRef(() => AuthService))
private readonly authService: AuthService,
private readonly billingService: BillingService
) {
const config = {
credentials: {
Expand Down Expand Up @@ -216,4 +221,73 @@ export class AwsMarketplaceService {
return "Failed to register. Please contact Amplication support";
}
}

private mapDimensionToPlan(dimension: string): {
billingPlan: BillingPlan;
billingPeriod: BillingPeriod;
} {
switch (dimension) {
case "essential_plan":
return {
billingPlan: BillingPlan.Essential,
billingPeriod: BillingPeriod.Monthly,
};
case "Subscription":
return {
billingPlan: BillingPlan.Pro,
billingPeriod: BillingPeriod.Monthly,
};
case "enterprise_plan":
return {
billingPlan: BillingPlan.Enterprise,
billingPeriod: BillingPeriod.Annually,
};
default:
throw new Error(`Unknown dimension ${dimension}`);
}
}

async completeAwsMarketplaceIntegration(
email: string,
accountId: string,
workspaceId: string
) {
try {
const pendingAwsMarketplaceIntegration =
await this.prismaService.awsMarketplaceIntegration.findFirst({
where: {
email,
account: null,
workspace: null,
},
});

if (pendingAwsMarketplaceIntegration) {
await this.prismaService.awsMarketplaceIntegration.update({
where: {
email,
},
data: {
accountId,
workspaceId,
},
});

const subscription = this.mapDimensionToPlan(
pendingAwsMarketplaceIntegration.dimension
);

await this.billingService.provisionNewSubscriptionForAwsMarketplaceIntegration(
workspaceId,
subscription.billingPlan,
subscription.billingPeriod
);
}
} catch (error) {
this.logger.error(
`Failed to complete AWS Marketplace integration for ${email}, accountId: ${accountId}, workspaceId: ${workspaceId}`,
error
);
}
}
}
22 changes: 22 additions & 0 deletions packages/amplication-server/src/core/billing/billing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { AmplicationLogger } from "@amplication/util/nestjs/logging";
import { Inject, Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import Stigg, {
BillingPeriod,
BooleanEntitlement,
MeteredEntitlement,
NumericEntitlement,
ReportUsageAck,
SubscriptionAddon,
SubscriptionStatus,
UsageUpdateBehavior,
} from "@stigg/node-server-sdk";
Expand Down Expand Up @@ -339,6 +341,26 @@ export class BillingService {
});
}

async provisionNewSubscriptionForAwsMarketplaceIntegration(
workspaceId: string,
planId: BillingPlan,
planBillingPeriod: BillingPeriod,
addons?: SubscriptionAddon[]
): Promise<void> {
if (!this.isBillingEnabled) {
return;
}

await this.stiggClient.provisionSubscription({
customerId: workspaceId,
planId,
skipTrial: true,
billingPeriod: planBillingPeriod,
// promotionCode: "AWS-MARKETPLACE",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to update this call to create a new subscription telling stigg that the payment is already been taken care of

addons,
});
}

//todo: wrap with a try catch and return an object with the details about the limitations
async validateSubscriptionPlanLimitationsForWorkspace({
workspaceId,
Expand Down
Loading