2024-07-18 10:56:27 -05:00
|
|
|
import { PUBLIC_IMMICH_BUY_HOST, PUBLIC_IMMICH_PAY_HOST } from '$env/static/public';
|
2024-07-26 10:34:35 -05:00
|
|
|
import type { ImmichProduct } from '$lib/constants';
|
2024-07-18 10:56:27 -05:00
|
|
|
import { serverConfig } from '$lib/stores/server-config.store';
|
|
|
|
|
import { setServerLicense, setUserLicense, type LicenseResponseDto } from '@immich/sdk';
|
|
|
|
|
import { get } from 'svelte/store';
|
2024-07-26 00:27:44 -04:00
|
|
|
import { loadUser } from './auth';
|
2024-07-18 10:56:27 -05:00
|
|
|
|
2024-07-26 10:34:35 -05:00
|
|
|
export const activateProduct = async (licenseKey: string, activationKey: string): Promise<LicenseResponseDto> => {
|
2024-07-26 00:27:44 -04:00
|
|
|
// Send server key to user activation if user is not admin
|
|
|
|
|
const user = await loadUser();
|
|
|
|
|
const isServerActivation = user?.isAdmin && licenseKey.search('IMSV') !== -1;
|
2024-07-18 10:56:27 -05:00
|
|
|
const licenseKeyDto = { licenseKey, activationKey };
|
2024-07-26 00:27:44 -04:00
|
|
|
return isServerActivation ? setServerLicense({ licenseKeyDto }) : setUserLicense({ licenseKeyDto });
|
2024-07-18 10:56:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getActivationKey = async (licenseKey: string): Promise<string> => {
|
|
|
|
|
const response = await fetch(new URL(`/api/v1/activate/${licenseKey}`, PUBLIC_IMMICH_PAY_HOST).href);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error('Failed to fetch activation key');
|
|
|
|
|
}
|
|
|
|
|
return response.text();
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-26 10:34:35 -05:00
|
|
|
export const getLicenseLink = (license: ImmichProduct) => {
|
2024-07-18 10:56:27 -05:00
|
|
|
const url = new URL('/', PUBLIC_IMMICH_BUY_HOST);
|
|
|
|
|
url.searchParams.append('productId', license);
|
2024-12-18 15:19:48 +01:00
|
|
|
url.searchParams.append('instanceUrl', get(serverConfig).externalDomain || globalThis.origin);
|
2024-07-18 10:56:27 -05:00
|
|
|
return url.href;
|
|
|
|
|
};
|