Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

feat: License service #492

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
"tiki"
],
"scripts": {
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
MiroBenicio marked this conversation as resolved.
Show resolved Hide resolved
"verify:ios": "cd ios && pod deintegrate && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
"verify:android": "cd android && ./gradlew clean build test && cd ..",
"verify:web": "npm run build",
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
"eslint": "eslint \"src/**/*.{ts,js}\"",
Expand All @@ -44,6 +40,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@mytiki/tiki-sdk-capacitor": "^0.3.6",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down
74 changes: 74 additions & 0 deletions src/License/LicenseService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) TIKI Inc.
* MIT license. See LICENSE file in root directory.
*/

import * as TikiSdkLicensing from '@mytiki/tiki-sdk-capacitor';
import { TitleRecord, CommonTags, Tag, Usecase, CommonUsecases } from '@mytiki/tiki-sdk-capacitor';

export class LicenseService {
private _userId?: string = 'default';
private _providerId?: string = 'default';
private _terms?: string = 'default';
private _expiry?: Date;

constructor() {}

set userId(value: string | undefined) {
this._userId = value || 'default';
}

set providerId(value: string | undefined) {
this._providerId = value || 'default';
}

set terms(value: string | undefined) {
this._terms = value || 'default';
}

set expiry(value: Date | undefined) {
this._expiry = value;
}

/**
* Initializes the SDK service for the specified user.
* @param userId - The user's unique identifier to initialize the SDK for.
* @param providerId - The company's unique identifier to initialize the SDK for.
* @returns A Promise that resolves when the initialization is complete.
*/
async initialize(userId?: string, providerId?: string): Promise<void> {
if ((!userId && !this._userId) || (!providerId && this._providerId))
throw new Error('UserId/ProviderId is necessary to initialize');

await TikiSdkLicensing.instance.initialize(userId! ?? this._userId, providerId! ?? this.providerId!);
}

/**
* Creates a new license for the SDK service.
* @param userId - The user's unique identifier to initialize the SDK for.
* @param terms - The use terms for that license
* @param expiry - The expiry date for that license
* @returns A Promise that resolves when the license creation is complete.
*/
async createLicense(userId?: string, terms?: string, expiry?: Date): Promise<void> {
const isInitialized = await TikiSdkLicensing.instance.isInitialized()

if(!isInitialized) this.initialize()

const titleRecord: TitleRecord = await TikiSdkLicensing.instance.createTitle(userId! ?? this._userId, [
Tag.common(CommonTags.PURCHASE_HISTORY),
]);
await TikiSdkLicensing.instance.createLicense(
titleRecord.id,
[
{
usecases: [Usecase.common(CommonUsecases.ATTRIBUTION)],
destinations: ['*'],
},
],
terms! ?? this._terms,
expiry! ?? this._expiry,
"Receipt data"
);
}
}
MiroBenicio marked this conversation as resolved.
Show resolved Hide resolved