mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 20:24:05 -08:00
fix(core): Make enterprise trial requests via the backend (no-changelog) (#9784)
This commit is contained in:
parent
f7d7404907
commit
876bcbb04c
|
@ -1,5 +1,5 @@
|
|||
import { Get, Post, RestController, GlobalScope } from '@/decorators';
|
||||
import { LicenseRequest } from '@/requests';
|
||||
import { AuthenticatedRequest, LicenseRequest } from '@/requests';
|
||||
import { LicenseService } from './license.service';
|
||||
|
||||
@RestController('/license')
|
||||
|
@ -11,6 +11,12 @@ export class LicenseController {
|
|||
return await this.licenseService.getLicenseData();
|
||||
}
|
||||
|
||||
@Post('/enterprise/request_trial')
|
||||
@GlobalScope('license:manage')
|
||||
async requestEnterpriseTrial(req: AuthenticatedRequest) {
|
||||
await this.licenseService.requestEnterpriseTrial(req.user);
|
||||
}
|
||||
|
||||
@Post('/activate')
|
||||
@GlobalScope('license:manage')
|
||||
async activateLicense(req: LicenseRequest.Activate) {
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
import { Service } from 'typedi';
|
||||
import axios from 'axios';
|
||||
|
||||
import { Logger } from '@/Logger';
|
||||
import { License } from '@/License';
|
||||
import { InternalHooks } from '@/InternalHooks';
|
||||
import type { User } from '@db/entities/User';
|
||||
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
||||
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
|
||||
import { UrlService } from '@/services/url.service';
|
||||
|
||||
type LicenseError = Error & { errorId?: keyof typeof LicenseErrors };
|
||||
|
||||
|
@ -24,6 +28,7 @@ export class LicenseService {
|
|||
private readonly license: License,
|
||||
private readonly internalHooks: InternalHooks,
|
||||
private readonly workflowRepository: WorkflowRepository,
|
||||
private readonly urlService: UrlService,
|
||||
) {}
|
||||
|
||||
async getLicenseData() {
|
||||
|
@ -45,6 +50,16 @@ export class LicenseService {
|
|||
};
|
||||
}
|
||||
|
||||
async requestEnterpriseTrial(user: User) {
|
||||
await axios.post('https://enterprise.n8n.io/enterprise-trial', {
|
||||
licenseType: 'enterprise',
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
email: user.email,
|
||||
instanceUrl: this.urlService.getWebhookBaseUrl(),
|
||||
});
|
||||
}
|
||||
|
||||
getManagementJwt(): string {
|
||||
return this.license.getManagementJwt();
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import nock from 'nock';
|
||||
import config from '@/config';
|
||||
import { RESPONSE_ERROR_MESSAGES } from '@/constants';
|
||||
import type { User } from '@db/entities/User';
|
||||
|
@ -47,6 +48,20 @@ describe('GET /license', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('POST /license/enterprise/request_trial', () => {
|
||||
nock('https://enterprise.n8n.io').post('/enterprise-trial').reply(200);
|
||||
|
||||
test('should work for instance owner', async () => {
|
||||
await authOwnerAgent.post('/license/enterprise/request_trial').expect(200);
|
||||
});
|
||||
|
||||
test('does not work for regular users', async () => {
|
||||
await authMemberAgent
|
||||
.post('/license/enterprise/request_trial')
|
||||
.expect(403, { status: 'error', message: RESPONSE_ERROR_MESSAGES.MISSING_SCOPE });
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /license/activate', () => {
|
||||
test('should work for instance owner', async () => {
|
||||
await authOwnerAgent
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { makeRestApiRequest, request } from '@/utils/apiUtils';
|
||||
import { makeRestApiRequest } from '@/utils/apiUtils';
|
||||
import type { IRestApiContext, UsageState } from '@/Interface';
|
||||
|
||||
export const getLicense = async (context: IRestApiContext): Promise<UsageState['data']> => {
|
||||
|
@ -16,21 +16,8 @@ export const renewLicense = async (context: IRestApiContext): Promise<UsageState
|
|||
return await makeRestApiRequest(context, 'POST', '/license/renew');
|
||||
};
|
||||
|
||||
export const requestLicenseTrial = async (data: {
|
||||
licenseType: 'enterprise';
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
instanceUrl: string;
|
||||
}): Promise<UsageState['data']> => {
|
||||
return await request({
|
||||
method: 'POST',
|
||||
baseURL: 'https://enterprise.n8n.io',
|
||||
endpoint: '/enterprise-trial',
|
||||
data,
|
||||
withCredentials: false,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
export const requestLicenseTrial = async (
|
||||
context: IRestApiContext,
|
||||
): Promise<UsageState['data']> => {
|
||||
return await makeRestApiRequest(context, 'POST', '/license/enterprise/request_trial');
|
||||
};
|
||||
|
|
|
@ -85,19 +85,7 @@ export const useUsageStore = defineStore('usage', () => {
|
|||
};
|
||||
|
||||
const requestEnterpriseLicenseTrial = async () => {
|
||||
if (!usersStore.currentUser) {
|
||||
throw new Error('User is not logged in');
|
||||
}
|
||||
|
||||
const data = await requestLicenseTrial({
|
||||
licenseType: 'enterprise',
|
||||
firstName: usersStore.currentUser.firstName ?? '',
|
||||
lastName: usersStore.currentUser.lastName ?? '',
|
||||
email: usersStore.currentUser.email ?? '',
|
||||
instanceUrl: window.location.origin,
|
||||
});
|
||||
|
||||
return data;
|
||||
await requestLicenseTrial(rootStore.getRestApiContext);
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue