feat: created new features API version
This commit is contained in:
@@ -1,17 +1,33 @@
|
||||
import { type ApiFeature, ApiFeatureType } from "@api/features/apiFeature.js";
|
||||
import {
|
||||
ApiFeatureType,
|
||||
type ApiFeatureV0,
|
||||
} from "@api/features/prevVersions/apiFeatureV0.js";
|
||||
import type { UpdateFeatureParams } from "@api/features/updateFeatureParams.js";
|
||||
import {
|
||||
FeatureType,
|
||||
type FeatureUsageType,
|
||||
FeatureUsageType,
|
||||
} from "@models/featureModels/featureEnums.js";
|
||||
import type { Feature } from "@models/featureModels/featureModels.js";
|
||||
import { AppEnv } from "@models/genModels/genEnums.js";
|
||||
import type { ApiFeatureV1 } from "../../api/features/apiFeatureV1.js";
|
||||
import type {
|
||||
CreateFeatureV1Params,
|
||||
UpdateFeatureV1Params,
|
||||
} from "../../api/models.js";
|
||||
import {
|
||||
AffectedResource,
|
||||
ApiVersionClass,
|
||||
applyResponseVersionChanges,
|
||||
LATEST_VERSION,
|
||||
} from "../../api/versionUtils/versionUtils.js";
|
||||
import type { CreditSchemaItem } from "../../models/featureModels/featureConfig/creditConfig.js";
|
||||
import { notNullish, nullish } from "../utils.js";
|
||||
|
||||
export const apiFeatureToDbFeature = ({
|
||||
apiFeature,
|
||||
originalFeature,
|
||||
}: {
|
||||
apiFeature: ApiFeature | UpdateFeatureParams;
|
||||
apiFeature: ApiFeatureV0 | UpdateFeatureParams;
|
||||
originalFeature?: Feature;
|
||||
}) => {
|
||||
// Replace body...
|
||||
@@ -35,10 +51,12 @@ export const apiFeatureToDbFeature = ({
|
||||
}
|
||||
|
||||
if (apiFeature.credit_schema) {
|
||||
newConfig.schema = apiFeature.credit_schema.map((credit: { metered_feature_id: string; credit_cost: number }) => ({
|
||||
metered_feature_id: credit.metered_feature_id,
|
||||
credit_amount: credit.credit_cost,
|
||||
}));
|
||||
newConfig.schema = apiFeature.credit_schema.map(
|
||||
(credit: { metered_feature_id: string; credit_cost: number }) => ({
|
||||
metered_feature_id: credit.metered_feature_id,
|
||||
credit_amount: credit.credit_cost,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -56,12 +74,153 @@ export const apiFeatureToDbFeature = ({
|
||||
} satisfies Feature;
|
||||
};
|
||||
|
||||
export const featureV1ToDbFeatureConfig = ({
|
||||
apiFeature,
|
||||
originalFeature,
|
||||
}: {
|
||||
apiFeature: UpdateFeatureV1Params;
|
||||
originalFeature: Feature;
|
||||
}) => {
|
||||
const type = apiFeature.type || originalFeature.type;
|
||||
|
||||
if (nullish(apiFeature.consumable) && nullish(apiFeature.credit_schema))
|
||||
return;
|
||||
|
||||
if (type === FeatureType.Boolean) return;
|
||||
|
||||
if (type === FeatureType.Metered) {
|
||||
const newUsageType = notNullish(apiFeature.consumable)
|
||||
? apiFeature.consumable
|
||||
? FeatureUsageType.Single
|
||||
: FeatureUsageType.Continuous
|
||||
: originalFeature.config?.usage_type;
|
||||
return {
|
||||
usage_type: newUsageType,
|
||||
};
|
||||
}
|
||||
|
||||
if (type === FeatureType.CreditSystem) {
|
||||
const newSchema = notNullish(apiFeature.credit_schema)
|
||||
? apiFeature.credit_schema.map(
|
||||
(credit: { metered_feature_id: string; credit_cost: number }) => ({
|
||||
metered_feature_id: credit.metered_feature_id,
|
||||
credit_amount: credit.credit_cost,
|
||||
}),
|
||||
)
|
||||
: originalFeature.config?.schema;
|
||||
return {
|
||||
schema: newSchema,
|
||||
usage_type: FeatureUsageType.Single,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
};
|
||||
|
||||
export const featureV1ToDbFeature = ({
|
||||
apiFeature,
|
||||
originalFeature,
|
||||
}: {
|
||||
apiFeature: ApiFeatureV1 | CreateFeatureV1Params;
|
||||
originalFeature?: Feature;
|
||||
}) => {
|
||||
// Replace body...
|
||||
const featureType = apiFeature.type;
|
||||
const eventNames = apiFeature.event_names;
|
||||
|
||||
const newConfig =
|
||||
featureType === FeatureType.Boolean
|
||||
? undefined
|
||||
: originalFeature?.config || {};
|
||||
|
||||
if (apiFeature.type === FeatureType.Metered) {
|
||||
newConfig.usage_type = apiFeature.consumable
|
||||
? FeatureUsageType.Single
|
||||
: FeatureUsageType.Continuous;
|
||||
}
|
||||
|
||||
if (apiFeature.credit_schema) {
|
||||
newConfig.usage_type = FeatureUsageType.Single;
|
||||
newConfig.schema = apiFeature.credit_schema.map(
|
||||
(credit: { metered_feature_id: string; credit_cost: number }) => ({
|
||||
metered_feature_id: credit.metered_feature_id,
|
||||
credit_amount: credit.credit_cost,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
internal_id: originalFeature?.internal_id ?? "",
|
||||
org_id: originalFeature?.org_id ?? "",
|
||||
created_at: originalFeature?.created_at ?? Date.now(),
|
||||
env: originalFeature?.env ?? AppEnv.Sandbox,
|
||||
|
||||
id: apiFeature.id ?? originalFeature?.id ?? "",
|
||||
name: apiFeature.name ?? originalFeature?.name ?? "",
|
||||
type: featureType,
|
||||
config: newConfig,
|
||||
archived:
|
||||
"archived" in apiFeature
|
||||
? apiFeature.archived
|
||||
: (originalFeature?.archived ?? false),
|
||||
event_names: eventNames ?? [],
|
||||
} satisfies Feature;
|
||||
};
|
||||
|
||||
/**
|
||||
* Converts a database feature to the V1 API format (latest format).
|
||||
*
|
||||
* Version handling:
|
||||
* - This function always returns ApiFeatureV1 (V2.0+ format)
|
||||
* - Automatic version transformation to older formats (V0) happens via V1.2_FeatureChange
|
||||
* - The transformation is applied by the middleware when handlers use resource: AffectedResource.Feature
|
||||
* - For API version V1_Beta and older, responses are automatically converted to ApiFeatureV0 format
|
||||
*/
|
||||
export const dbToApiFeatureV1 = ({
|
||||
dbFeature,
|
||||
targetVersion,
|
||||
}: {
|
||||
dbFeature: Feature;
|
||||
targetVersion?: ApiVersionClass;
|
||||
}) => {
|
||||
const result = {
|
||||
id: dbFeature.id,
|
||||
name: dbFeature.name,
|
||||
type: dbFeature.type,
|
||||
consumable:
|
||||
dbFeature.type === FeatureType.CreditSystem ||
|
||||
dbFeature.config?.usage_type === FeatureUsageType.Single,
|
||||
|
||||
credit_schema: dbFeature.config?.schema?.map(
|
||||
(schema: CreditSchemaItem) => ({
|
||||
metered_feature_id: schema.metered_feature_id,
|
||||
credit_cost: schema.credit_amount,
|
||||
}),
|
||||
),
|
||||
event_names: dbFeature.event_names,
|
||||
archived: dbFeature.archived,
|
||||
|
||||
display: dbFeature.display
|
||||
? {
|
||||
singular: dbFeature.display.singular,
|
||||
plural: dbFeature.display.plural,
|
||||
}
|
||||
: undefined,
|
||||
} satisfies ApiFeatureV1;
|
||||
|
||||
return applyResponseVersionChanges({
|
||||
input: result,
|
||||
targetVersion: targetVersion ?? new ApiVersionClass(LATEST_VERSION),
|
||||
resource: AffectedResource.Feature,
|
||||
});
|
||||
};
|
||||
|
||||
// export const fromApiFeature = ({
|
||||
// apiFeature,
|
||||
// orgId,
|
||||
// env,
|
||||
// }: {
|
||||
// apiFeature: ApiFeature;
|
||||
// apiFeature: ApiFeatureV0;
|
||||
// orgId: string;
|
||||
// env: AppEnv;
|
||||
// }) => {
|
||||
|
||||
Reference in New Issue
Block a user