-
Notifications
You must be signed in to change notification settings - Fork 27
WIP: Add licensing #368
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
Open
imsitnikov
wants to merge
4
commits into
main
Choose a base branch
from
DLAPI-563-licensing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
WIP: Add licensing #368
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
52 changes: 52 additions & 0 deletions
52
src/db/migrations/20251019110620_add_table_license_limits.ts
This file contains hidden or 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,52 @@ | ||
| import type {Knex} from 'knex'; | ||
|
|
||
| export async function up(knex: Knex): Promise<void> { | ||
| return knex.raw(` | ||
| CREATE TABLE license_limits ( | ||
| license_limit_id BIGINT NOT NULL PRIMARY KEY DEFAULT get_id(), | ||
| tenant_id TEXT NOT NULL DEFAULT 'common' REFERENCES tenants (tenant_id) ON UPDATE CASCADE ON DELETE CASCADE, | ||
| limit_value INT NOT NULL, | ||
| started_at TIMESTAMPTZ NOT NULL, | ||
| created_by TEXT NOT NULL, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| updated_by TEXT NOT NULL, | ||
| updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
|
|
||
| CREATE INDEX license_limits_tenant_id_started_at_idx ON license_limits(tenant_id, started_at); | ||
|
|
||
| CREATE TYPE LICENSE_TYPE AS ENUM ('creator', 'visitor'); | ||
|
|
||
| CREATE TABLE license_assignments ( | ||
| license_assignment_id BIGINT NOT NULL PRIMARY KEY DEFAULT get_id(), | ||
| tenant_id TEXT NOT NULL DEFAULT 'common' REFERENCES tenants (tenant_id) ON UPDATE CASCADE ON DELETE CASCADE, | ||
| user_id TEXT NOT NULL, | ||
| license_type LICENSE_TYPE NOT NULL, | ||
| expires_at TIMESTAMPTZ, | ||
| created_by TEXT NOT NULL, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
| updated_by TEXT NOT NULL, | ||
| updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| ); | ||
|
|
||
| CREATE UNIQUE INDEX license_assignments_tenant_id_user_id_idx ON license_assignments(tenant_id, user_id); | ||
| CREATE INDEX license_assignments_expires_at_idx ON license_assignments(expires_at); | ||
| CREATE INDEX license_assignments_created_at_idx ON license_assignments(created_at); | ||
| CREATE INDEX license_assignments_updated_at_idx ON license_assignments(updated_at); | ||
|
|
||
| `); | ||
| } | ||
|
|
||
| export async function down(knex: Knex): Promise<void> { | ||
| return knex.raw(` | ||
| DROP INDEX license_assignments_updated_at_idx; | ||
| DROP INDEX license_assignments_created_at_idx; | ||
| DROP INDEX license_assignments_expires_at_idx; | ||
| DROP INDEX license_assignments_tenant_id_user_id_idx; | ||
| DROP TABLE license_assignments; | ||
| DROP TYPE LICENSE_TYPE; | ||
|
|
||
| DROP INDEX license_limits_tenant_id_started_at_idx; | ||
| DROP TABLE license_limits; | ||
| `); | ||
| } |
This file contains hidden or 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,38 @@ | ||
| import {Model} from '../../..'; | ||
| import {mapValuesToSnakeCase} from '../../../../utils'; | ||
|
|
||
| import {LicenseType} from './types'; | ||
|
|
||
| export const LicenseAssignmentColumn = { | ||
| LicenseAssignmentId: 'licenseAssignmentId', | ||
| TenantId: 'tenantId', | ||
| UserId: 'userId', | ||
| LicenseType: 'licenseType', | ||
| ExpiresAt: 'expiresAt', | ||
| CreatedBy: 'createdBy', | ||
| CreatedAt: 'createdAt', | ||
| UpdatedBy: 'updatedBy', | ||
| UpdatedAt: 'updatedAt', | ||
| } as const; | ||
|
|
||
| export const LicenseAssignmentColumnRaw = mapValuesToSnakeCase(LicenseAssignmentColumn); | ||
|
|
||
| export class LicenseAssignment extends Model { | ||
| static get tableName() { | ||
| return 'license_assignments'; | ||
| } | ||
|
|
||
| static get idColumn() { | ||
| return LicenseAssignmentColumn.LicenseAssignmentId; | ||
| } | ||
|
|
||
| [LicenseAssignmentColumn.LicenseAssignmentId]!: string; | ||
| [LicenseAssignmentColumn.TenantId]!: string; | ||
| [LicenseAssignmentColumn.UserId]!: string; | ||
| [LicenseAssignmentColumn.LicenseType]!: `${LicenseType}`; | ||
| [LicenseAssignmentColumn.ExpiresAt]!: Nullable<string>; | ||
| [LicenseAssignmentColumn.CreatedBy]!: string; | ||
| [LicenseAssignmentColumn.CreatedAt]!: string; | ||
| [LicenseAssignmentColumn.UpdatedBy]!: string; | ||
| [LicenseAssignmentColumn.UpdatedAt]!: string; | ||
| } | ||
This file contains hidden or 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 @@ | ||
| export * from './license-assignment-with-is-active'; |
27 changes: 27 additions & 0 deletions
27
src/db/models/new/license-assignment/presentations/license-assignment-with-is-active.ts
This file contains hidden or 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,27 @@ | ||
| import {QueryBuilder, TransactionOrKnex, raw} from 'objection'; | ||
|
|
||
| import {CURRENT_TIMESTAMP} from '../../../../../const'; | ||
| import {LicenseAssignment, LicenseAssignmentColumnRaw} from '../index'; | ||
|
|
||
| export class LicenseAssignmentWithIsActive extends LicenseAssignment { | ||
| protected static get selectedColumns() { | ||
| return [ | ||
| '*', | ||
| raw(`?? > ${CURRENT_TIMESTAMP} OR ?? IS NULL`, [ | ||
| LicenseAssignmentColumnRaw.ExpiresAt, | ||
| LicenseAssignmentColumnRaw.ExpiresAt, | ||
| ]).as('is_active'), | ||
| ]; | ||
| } | ||
|
|
||
| static getSelectQuery(trx: TransactionOrKnex) { | ||
| const query = LicenseAssignment.query(trx).select(this.selectedColumns); | ||
|
|
||
| return query as QueryBuilder< | ||
| LicenseAssignmentWithIsActive, | ||
| LicenseAssignmentWithIsActive[] | ||
| >; | ||
| } | ||
|
|
||
| isActive!: boolean; | ||
| } |
This file contains hidden or 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,4 @@ | ||
| export enum LicenseType { | ||
| Creator = 'creator', | ||
| Visitor = 'visitor', | ||
| } |
This file contains hidden or 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,34 @@ | ||
| import {Model} from '../../..'; | ||
| import {mapValuesToSnakeCase} from '../../../../utils'; | ||
|
|
||
| export const LicenseLimitColumn = { | ||
| LicenseLimitId: 'licenseLimitId', | ||
| TenantId: 'tenantId', | ||
| LimitValue: 'limitValue', | ||
| StartedAt: 'startedAt', | ||
| CreatedBy: 'createdBy', | ||
| CreatedAt: 'createdAt', | ||
| UpdatedBy: 'updatedBy', | ||
| UpdatedAt: 'updatedAt', | ||
| } as const; | ||
|
|
||
| export const LicenseLimitColumnRaw = mapValuesToSnakeCase(LicenseLimitColumn); | ||
|
|
||
| export class LicenseLimit extends Model { | ||
| static get tableName() { | ||
| return 'license_limits'; | ||
| } | ||
|
|
||
| static get idColumn() { | ||
| return LicenseLimitColumn.LicenseLimitId; | ||
| } | ||
|
|
||
| [LicenseLimitColumn.LicenseLimitId]!: string; | ||
| [LicenseLimitColumn.TenantId]!: string; | ||
| [LicenseLimitColumn.LimitValue]!: number; | ||
| [LicenseLimitColumn.StartedAt]!: string; | ||
| [LicenseLimitColumn.CreatedBy]!: string; | ||
| [LicenseLimitColumn.CreatedAt]!: string; | ||
| [LicenseLimitColumn.UpdatedBy]!: string; | ||
| [LicenseLimitColumn.UpdatedAt]!: string; | ||
| } |
This file contains hidden or 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 @@ | ||
| export * from './license-limit-with-started-in-future'; |
26 changes: 26 additions & 0 deletions
26
src/db/models/new/license-limit/presentations/license-limit-with-started-in-future.ts
This file contains hidden or 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,26 @@ | ||
| import {QueryBuilder, TransactionOrKnex, raw} from 'objection'; | ||
|
|
||
| import {CURRENT_TIMESTAMP} from '../../../../../const'; | ||
| import {LicenseLimit, LicenseLimitColumnRaw} from '../index'; | ||
|
|
||
| export class LicenseLimitWithStartedInFuture extends LicenseLimit { | ||
| protected static get selectedColumns() { | ||
| return [ | ||
| '*', | ||
| raw(`?? > ${CURRENT_TIMESTAMP}`, [LicenseLimitColumnRaw.StartedAt]).as( | ||
| 'started_in_future', | ||
| ), | ||
| ]; | ||
| } | ||
|
|
||
| static getSelectQuery(trx: TransactionOrKnex) { | ||
| const query = LicenseLimit.query(trx).select(this.selectedColumns); | ||
|
|
||
| return query as QueryBuilder< | ||
| LicenseLimitWithStartedInFuture, | ||
| LicenseLimitWithStartedInFuture[] | ||
| >; | ||
| } | ||
|
|
||
| startedInFuture!: boolean; | ||
| } |
This file contains hidden or 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,18 @@ | ||
| type SnakeCase<S extends string> = S extends `${infer T}${infer U}` | ||
| ? `${T extends Capitalize<T> ? '_' : ''}${Lowercase<T>}${SnakeCase<U>}` | ||
| : S; | ||
|
|
||
| type MapToSnakeCase<T extends Record<string, string>> = { | ||
| [K in keyof T]: SnakeCase<T[K]>; | ||
| }; | ||
|
|
||
| const toSnakeCase = (str: string): string => | ||
| str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); | ||
|
|
||
| export const mapValuesToSnakeCase = <T extends Record<string, string>>( | ||
| obj: T, | ||
| ): MapToSnakeCase<T> => { | ||
| return Object.fromEntries( | ||
| Object.entries(obj).map(([key, value]) => [key, toSnakeCase(value)]), | ||
| ) as MapToSnakeCase<T>; | ||
| }; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool :)