-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): support selfhost licenses
- Loading branch information
Showing
17 changed files
with
873 additions
and
41 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/backend/server/migrations/20241202082025_licenses/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
-- CreateTable | ||
CREATE TABLE "licenses" ( | ||
"key" VARCHAR NOT NULL, | ||
"installed_at" TIMESTAMPTZ(3) DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "licenses_pkey" PRIMARY KEY ("key") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "installed_licenses" ( | ||
"key" VARCHAR NOT NULL, | ||
"workspace_id" VARCHAR NOT NULL, | ||
"installed_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"revalidated_at" TIMESTAMPTZ(3), | ||
|
||
CONSTRAINT "installed_licenses_pkey" PRIMARY KEY ("key") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "installed_licenses_workspace_id_key" ON "installed_licenses"("workspace_id"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/backend/server/src/plugins/payment/license/controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import { Body, Controller, Param, Post } from '@nestjs/common'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { z } from 'zod'; | ||
|
||
import { | ||
InvalidLicenseToActivate, | ||
InvalidLicenseUpdateParams, | ||
LicenseNotFound, | ||
Mutex, | ||
} from '../../../fundamentals'; | ||
import { SelfhostTeamSubscriptionManager } from '../manager/selfhost'; | ||
import { SubscriptionService } from '../service'; | ||
import { | ||
SubscriptionPlan, | ||
SubscriptionRecurring, | ||
SubscriptionStatus, | ||
} from '../types'; | ||
|
||
const UpdateSeatsParams = z.object({ | ||
seats: z.number().min(1), | ||
}); | ||
|
||
const UpdateRecurringParams = z.object({ | ||
recurring: z.enum([ | ||
SubscriptionRecurring.Monthly, | ||
SubscriptionRecurring.Yearly, | ||
]), | ||
}); | ||
|
||
@Controller('/api/team/licenses') | ||
export class LicenseController { | ||
constructor( | ||
private readonly db: PrismaClient, | ||
private readonly mutex: Mutex, | ||
private readonly subscription: SubscriptionService, | ||
private readonly manager: SelfhostTeamSubscriptionManager | ||
) {} | ||
|
||
@Post('/:license/activate') | ||
async activate(@Param('license') key: string) { | ||
await using lock = await this.mutex.lock(`license-activation:${key}`); | ||
|
||
if (!lock) { | ||
throw new InvalidLicenseToActivate(); | ||
} | ||
|
||
const license = await this.db.license.findUnique({ | ||
where: { | ||
key, | ||
}, | ||
}); | ||
|
||
if (!license) { | ||
throw new InvalidLicenseToActivate(); | ||
} | ||
|
||
const subscription = await this.manager.getSubscription({ | ||
key: license.key, | ||
plan: SubscriptionPlan.SelfHostedTeam, | ||
}); | ||
|
||
if ( | ||
!subscription || | ||
license.installedAt || | ||
subscription.status !== SubscriptionStatus.Active | ||
) { | ||
throw new InvalidLicenseToActivate(); | ||
} | ||
|
||
await this.db.license.update({ | ||
where: { | ||
key, | ||
}, | ||
data: { | ||
installedAt: new Date(), | ||
}, | ||
}); | ||
|
||
return { | ||
quota: {}, | ||
endAt: subscription.end?.getTime(), | ||
}; | ||
} | ||
|
||
@Post('/:license/deactivate') | ||
async deactivate(@Param('license') key: string) { | ||
await this.db.license.update({ | ||
where: { | ||
key, | ||
}, | ||
data: { | ||
installedAt: null, | ||
}, | ||
}); | ||
|
||
return { | ||
success: true, | ||
}; | ||
} | ||
|
||
@Post('/:license/seats') | ||
async updateSeats( | ||
@Param('license') key: string, | ||
@Body() body: z.infer<typeof UpdateSeatsParams> | ||
) { | ||
const parseResult = UpdateSeatsParams.safeParse(body); | ||
|
||
if (parseResult.error) { | ||
throw new InvalidLicenseUpdateParams({ | ||
reason: parseResult.error.message, | ||
}); | ||
} | ||
|
||
const license = await this.db.license.findUnique({ | ||
where: { | ||
key, | ||
}, | ||
}); | ||
|
||
if (!license) { | ||
throw new LicenseNotFound(); | ||
} | ||
|
||
await this.subscription.updateSubscriptionQuantity( | ||
{ | ||
key: license.key, | ||
plan: SubscriptionPlan.SelfHostedTeam, | ||
}, | ||
parseResult.data.seats | ||
); | ||
} | ||
|
||
@Post('/:license/recurring') | ||
async updateRecurring( | ||
@Param('license') key: string, | ||
@Body() body: z.infer<typeof UpdateRecurringParams> | ||
) { | ||
const parseResult = UpdateRecurringParams.safeParse(body); | ||
|
||
if (parseResult.error) { | ||
throw new InvalidLicenseUpdateParams({ | ||
reason: parseResult.error.message, | ||
}); | ||
} | ||
|
||
const license = await this.db.license.findUnique({ | ||
where: { | ||
key, | ||
}, | ||
}); | ||
|
||
if (!license) { | ||
throw new LicenseNotFound(); | ||
} | ||
|
||
await this.subscription.updateSubscriptionRecurring( | ||
{ | ||
key: license.key, | ||
plan: SubscriptionPlan.SelfHostedTeam, | ||
}, | ||
parseResult.data.recurring | ||
); | ||
} | ||
} |
Oops, something went wrong.