feat(medusa): PublishableAPI keys model (#2543)

This commit is contained in:
Frane Polić
2022-11-10 10:14:06 +01:00
committed by GitHub
parent 7914f2a82a
commit aa8eba38e9
6 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
import { FlagSettings } from "../../types/feature-flags"
const PublishableAPIKeysFeatureFlag: FlagSettings = {
key: "publishable_api_keys",
default_val: false,
env_key: "MEDUSA_FF_PUBLISHABLE_API_KEYS",
description: "[WIP] Enable the publishable API keys feature",
}
export default PublishableAPIKeysFeatureFlag

View File

@@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm"
import PublishableAPIKeysFeatureFlag from "../loaders/feature-flags/publishable-api-keys"
export const featureFlag = PublishableAPIKeysFeatureFlag.key
export class publishableApiKey1667815005070 implements MigrationInterface {
name = "publishableApiKey1667815005070"
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "publishable_api_key_sales_channel" ("sales_channel_id" character varying NOT NULL, "publishable_key_id" character varying NOT NULL, CONSTRAINT "PK_68eaeb14bdac8954460054c567c" PRIMARY KEY ("sales_channel_id", "publishable_key_id"))`
)
await queryRunner.query(
`CREATE TABLE "publishable_api_key" ("id" character varying NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "created_by" character varying, "revoked_by" character varying, "revoked_at" TIMESTAMP WITH TIME ZONE, CONSTRAINT "PK_9e613278673a87de92c606b4494" PRIMARY KEY ("id"))`
)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "publishable_api_key"`)
await queryRunner.query(`DROP TABLE "publishable_api_key_sales_channel"`)
}
}

View File

@@ -52,6 +52,8 @@ export * from "./product-tax-rate"
export * from "./product-type"
export * from "./product-type-tax-rate"
export * from "./product-variant"
export * from "./publishable-api-key"
export * from "./publishable-api-key-sales-channel"
export * from "./refund"
export * from "./region"
export * from "./return"

View File

@@ -0,0 +1,29 @@
import { PrimaryColumn } from "typeorm"
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
import PublishableAPIKeysFeatureFlag from "../loaders/feature-flags/publishable-api-keys"
@FeatureFlagEntity(PublishableAPIKeysFeatureFlag.key)
export class PublishableApiKeySalesChannel {
@PrimaryColumn()
sales_channel_id: string
@PrimaryColumn()
publishable_key_id: string
}
/**
* @schema publishable_api_key_sales_channel
* title: "Publishable API key sales channel"
* description: "Holds mapping between Publishable API keys and Sales Channels"
* x-resourceId: publishable_api_key_sales_channel
* properties:
* sales_channel_id:
* type: string
* description: The sales channel's ID
* example: sc_01G1G5V21KADXNGH29BJMAJ4B4
* publishable_key_id:
* type: string
* description: The publishable API key's ID
* example: pak_01G1G5V21KADXNGH29BJMAJ4B4
*/

View File

@@ -0,0 +1,62 @@
import { BeforeInsert, Column } from "typeorm"
import { BaseEntity } from "../interfaces"
import { resolveDbType } from "../utils/db-aware-column"
import { generateEntityId } from "../utils"
import { FeatureFlagEntity } from "../utils/feature-flag-decorators"
import PublishableAPIKeysFeatureFlag from "../loaders/feature-flags/publishable-api-keys"
@FeatureFlagEntity(PublishableAPIKeysFeatureFlag.key)
export class PublishableApiKey extends BaseEntity {
@Column({ type: "varchar", nullable: true })
created_by: string | null
@Column({ type: "varchar", nullable: true })
revoked_by: string | null
@Column({ type: resolveDbType("timestamptz"), nullable: true })
revoked_at?: Date
@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "pubkey")
}
}
/**
* @schema publishable_api_key
* title: "Publishable API key"
* description: "Publishable API key defines scopes (i.e. resources) that are available within a request."
* x-resourceId: publishable_api_key
* properties:
* id:
* type: string
* description: The key's ID
* example: pak_01G1G5V27GYX4QXNARRQCW1N8T
* created_by:
* type: string
* description: "The unique identifier of the user that created the key."
* example: usr_01G1G5V26F5TB3GPAPNJ8X1S3V
* created_by_user:
* description: A user object. Available if the relation `created_by_user` is expanded.
* type: object
* created_at:
* type: string
* description: "The date with timezone at which the resource was created."
* format: date-time
* revoked_by:
* type: string
* description: "The unique identifier of the user that revoked the key."
* example: usr_01G1G5V26F5TB3GPAPNJ8X1S3V
* revoked_by_user:
* description: A user object. Available if the relation `revoked_by_user` is expanded.
* type: object
* revoked_at:
* type: string
* description: "The date with timezone at which the key was revoked."
* format: date-time
* updated_at:
* type: string
* description: "The date with timezone at which the resource was updated."
* format: date-time
*/

View File

@@ -0,0 +1,6 @@
import { EntityRepository, Repository } from "typeorm"
import { PublishableApiKey } from "../models/publishable-api-key"
@EntityRepository(PublishableApiKey)
export class PublishableApiKeyRepository extends Repository<PublishableApiKey> {}