Skip to content

Commit

Permalink
chore: import IUser instead of User for auth (#5269)
Browse files Browse the repository at this point in the history
## About the changes
A very subtle change in the way we import IUser makes a huge difference
because previously, instead of importing IUser interface we were
importing User and naming it IUser here:

https://github.com/Unleash/unleash/blob/6f8f21fd488cfbf53a3526ffa138e40c3a43e27c/src/lib/routes/unleash-types.ts#L2
whereas the correct way of importing the interface is:

https://github.com/Unleash/unleash/blob/eec64b119e133813e4f4c4a42e9bdd0a8db65259/src/lib/routes/unleash-types.ts#L2
  • Loading branch information
gastonfournier authored Nov 6, 2023
1 parent 5c3fe63 commit f16ad4e
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import User from '../../types/user';
import { IUser } from '../../types/user';

export interface IChangeRequestAccessReadModel {
canBypassChangeRequest(
project: string,
environment: string,
user?: User,
user?: IUser,
): Promise<boolean>;
canBypassChangeRequestForProject(
project: string,
user?: User,
user?: IUser,
): Promise<boolean>;
isChangeRequestsEnabled(
project: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { IDependentFeaturesStore } from './dependent-features-store-type';
import { FeatureDependency, FeatureDependencyId } from './dependent-features';
import { IDependentFeaturesReadModel } from './dependent-features-read-model-type';
import { EventService } from '../../services';
import { User } from '../../server-impl';
import { IUser } from '../../server-impl';
import { SKIP_CHANGE_REQUEST } from '../../types';
import { IChangeRequestAccessReadModel } from '../change-request-access-service/change-request-access-read-model';
import { extractUsernameFromUser } from '../../util';
Expand Down Expand Up @@ -72,7 +72,7 @@ export class DependentFeaturesService {
async upsertFeatureDependency(
{ child, projectId }: { child: string; projectId: string },
dependentFeature: CreateDependentFeatureSchema,
user: User,
user: IUser,
): Promise<void> {
await this.stopWhenChangeRequestsEnabled(projectId, user);

Expand Down Expand Up @@ -158,7 +158,7 @@ export class DependentFeaturesService {
async deleteFeatureDependency(
dependency: FeatureDependencyId,
projectId: string,
user: User,
user: IUser,
): Promise<void> {
await this.stopWhenChangeRequestsEnabled(projectId, user);

Expand Down Expand Up @@ -187,7 +187,7 @@ export class DependentFeaturesService {
async deleteFeaturesDependencies(
features: string[],
projectId: string,
user: User,
user: IUser,
): Promise<void> {
await this.stopWhenChangeRequestsEnabled(projectId, user);

Expand Down Expand Up @@ -222,7 +222,7 @@ export class DependentFeaturesService {
return this.dependentFeaturesReadModel.hasAnyDependencies();
}

private async stopWhenChangeRequestsEnabled(project: string, user?: User) {
private async stopWhenChangeRequestsEnabled(project: string, user?: IUser) {
const canBypass =
await this.changeRequestAccessReadModel.canBypassChangeRequestForProject(
project,
Expand Down
32 changes: 16 additions & 16 deletions src/lib/features/export-import-toggles/export-import-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
ImportTogglesSchema,
ImportTogglesValidateSchema,
} from '../../openapi';
import User from '../../types/user';
import { IUser } from '../../types/user';
import { BadDataError } from '../../error';
import { extractUsernameFromUser } from '../../util';
import {
Expand Down Expand Up @@ -56,10 +56,10 @@ import { ISegmentService } from '../../segments/segment-service-interface';
export type IImportService = {
validate(
dto: ImportTogglesSchema,
user: User,
user: IUser,
): Promise<ImportTogglesValidateSchema>;

import(dto: ImportTogglesSchema, user: User): Promise<void>;
import(dto: ImportTogglesSchema, user: IUser): Promise<void>;
};

export type IExportService = {
Expand Down Expand Up @@ -184,7 +184,7 @@ export default class ExportImportService

async validate(
dto: ImportTogglesSchema,
user: User,
user: IUser,
mode = 'regular' as Mode,
): Promise<ImportTogglesValidateSchema> {
const [
Expand Down Expand Up @@ -249,7 +249,7 @@ export default class ExportImportService

async importVerify(
dto: ImportTogglesSchema,
user: User,
user: IUser,
mode = 'regular' as Mode,
): Promise<void> {
await Promise.all([
Expand All @@ -264,7 +264,7 @@ export default class ExportImportService

async importFeatureData(
dto: ImportTogglesSchema,
user: User,
user: IUser,
): Promise<void> {
await this.createOrUpdateToggles(dto, user);
await this.importToggleVariants(dto, user);
Expand All @@ -273,7 +273,7 @@ export default class ExportImportService
await this.importContextFields(dto, user);
}

async import(dto: ImportTogglesSchema, user: User): Promise<void> {
async import(dto: ImportTogglesSchema, user: IUser): Promise<void> {
const cleanedDto = await this.cleanData(dto);

await this.importVerify(cleanedDto, user);
Expand All @@ -291,15 +291,15 @@ export default class ExportImportService

async importEnvironmentData(
dto: ImportTogglesSchema,
user: User,
user: IUser,
): Promise<void> {
await this.deleteStrategies(dto);
await this.importStrategies(dto, user);
await this.importToggleStatuses(dto, user);
await this.importDependencies(dto, user);
}

private async importDependencies(dto: ImportTogglesSchema, user: User) {
private async importDependencies(dto: ImportTogglesSchema, user: IUser) {
await Promise.all(
(dto.data.dependencies || []).flatMap((dependency) => {
const projectId = dto.data.features.find(
Expand All @@ -319,7 +319,7 @@ export default class ExportImportService
);
}

private async importToggleStatuses(dto: ImportTogglesSchema, user: User) {
private async importToggleStatuses(dto: ImportTogglesSchema, user: IUser) {
await Promise.all(
(dto.data.featureEnvironments || []).map((featureEnvironment) =>
this.featureToggleService.updateEnabled(
Expand All @@ -334,7 +334,7 @@ export default class ExportImportService
);
}

private async importStrategies(dto: ImportTogglesSchema, user: User) {
private async importStrategies(dto: ImportTogglesSchema, user: IUser) {
const hasFeatureName = (
featureStrategy: FeatureStrategySchema,
): featureStrategy is WithRequired<
Expand Down Expand Up @@ -371,7 +371,7 @@ export default class ExportImportService
);
}

private async importTags(dto: ImportTogglesSchema, user: User) {
private async importTags(dto: ImportTogglesSchema, user: IUser) {
await this.importTogglesStore.deleteTagsForFeatures(
dto.data.features.map((feature) => feature.name),
);
Expand All @@ -391,7 +391,7 @@ export default class ExportImportService
}
}

private async importContextFields(dto: ImportTogglesSchema, user: User) {
private async importContextFields(dto: ImportTogglesSchema, user: IUser) {
const newContextFields = (await this.getNewContextFields(dto)) || [];
await Promise.all(
newContextFields.map((contextField) =>
Expand All @@ -408,7 +408,7 @@ export default class ExportImportService
);
}

private async importTagTypes(dto: ImportTogglesSchema, user: User) {
private async importTagTypes(dto: ImportTogglesSchema, user: IUser) {
const newTagTypes = await this.getNewTagTypes(dto);
return Promise.all(
newTagTypes.map((tagType) => {
Expand All @@ -422,7 +422,7 @@ export default class ExportImportService
);
}

private async importToggleVariants(dto: ImportTogglesSchema, user: User) {
private async importToggleVariants(dto: ImportTogglesSchema, user: IUser) {
const featureEnvsWithVariants =
dto.data.featureEnvironments?.filter(
(featureEnvironment) =>
Expand All @@ -444,7 +444,7 @@ export default class ExportImportService
);
}

private async createOrUpdateToggles(dto: ImportTogglesSchema, user: User) {
private async createOrUpdateToggles(dto: ImportTogglesSchema, user: IUser) {
const existingFeatures = await this.getExistingProjectFeatures(dto);
const username = extractUsernameFromUser(user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IImportTogglesStore } from './import-toggles-store-type';
import { AccessService, ContextService, TagTypeService } from '../../services';
import { ContextFieldSchema, ImportTogglesSchema } from '../../openapi';
import { ITagType } from '../../types/stores/tag-type-store';
import User from '../../types/user';
import { IUser } from '../../types/user';
import {
CREATE_CONTEXT_FIELD,
CREATE_FEATURE,
Expand Down Expand Up @@ -67,7 +67,7 @@ export class ImportPermissionsService {

async getMissingPermissions(
dto: ImportTogglesSchema,
user: User,
user: IUser,
mode: Mode,
): Promise<string[]> {
const [
Expand Down Expand Up @@ -140,7 +140,7 @@ export class ImportPermissionsService {

async verifyPermissions(
dto: ImportTogglesSchema,
user: User,
user: IUser,
mode: Mode,
): Promise<void> {
const missingPermissions = await this.getMissingPermissions(
Expand Down
35 changes: 17 additions & 18 deletions src/lib/features/feature-toggle/feature-toggle-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
IFeatureOverview,
IFeatureStrategy,
IFeatureTagStore,
IFeatureToggleClient,
IFeatureToggleClientStore,
IFeatureToggleQuery,
IFeatureToggleStore,
Expand Down Expand Up @@ -92,7 +91,7 @@ import {
getProjectDefaultStrategy,
} from '../playground/feature-evaluator/helpers';
import { AccessService } from '../../services/access-service';
import { User } from '../../server-impl';
import { IUser } from '../../server-impl';
import { IFeatureProjectUserParams } from './feature-toggle-controller';
import { unique } from '../../util/unique';
import { ISegmentService } from 'lib/segments/segment-service-interface';
Expand Down Expand Up @@ -464,7 +463,7 @@ class FeatureToggleService {
context: IFeatureStrategyContext,
sortOrders: SetStrategySortOrderSchema,
createdBy: string,
user?: User,
user?: IUser,
): Promise<Saved<any>> {
await this.stopWhenChangeRequestsEnabled(
context.projectId,
Expand Down Expand Up @@ -548,7 +547,7 @@ class FeatureToggleService {
strategyConfig: Unsaved<IStrategyConfig>,
context: IFeatureStrategyContext,
createdBy: string,
user?: User,
user?: IUser,
): Promise<Saved<IStrategyConfig>> {
await this.stopWhenChangeRequestsEnabled(
context.projectId,
Expand Down Expand Up @@ -671,7 +670,7 @@ class FeatureToggleService {
updates: Partial<IStrategyConfig>,
context: IFeatureStrategyContext,
userName: string,
user?: User,
user?: IUser,
): Promise<Saved<IStrategyConfig>> {
await this.stopWhenChangeRequestsEnabled(
context.projectId,
Expand Down Expand Up @@ -835,7 +834,7 @@ class FeatureToggleService {
id: string,
context: IFeatureStrategyContext,
createdBy: string,
user?: User,
user?: IUser,
): Promise<void> {
await this.stopWhenChangeRequestsEnabled(
context.projectId,
Expand Down Expand Up @@ -1488,7 +1487,7 @@ class FeatureToggleService {

async archiveToggle(
featureName: string,
user: User,
user: IUser,
projectId?: string,
): Promise<void> {
if (projectId) {
Expand Down Expand Up @@ -1542,7 +1541,7 @@ class FeatureToggleService {

async archiveToggles(
featureNames: string[],
user: User,
user: IUser,
projectId: string,
): Promise<void> {
await this.stopWhenChangeRequestsEnabled(projectId, undefined, user);
Expand Down Expand Up @@ -1641,7 +1640,7 @@ class FeatureToggleService {
environment: string,
enabled: boolean,
createdBy: string,
user?: User,
user?: IUser,
shouldActivateDisabledStrategies = false,
): Promise<void> {
await Promise.all(
Expand All @@ -1665,7 +1664,7 @@ class FeatureToggleService {
environment: string,
enabled: boolean,
createdBy: string,
user?: User,
user?: IUser,
shouldActivateDisabledStrategies = false,
): Promise<FeatureToggle> {
await this.stopWhenChangeRequestsEnabled(project, environment, user);
Expand Down Expand Up @@ -2055,7 +2054,7 @@ class FeatureToggleService {
featureName: string,
project: string,
newVariants: Operation[],
user: User,
user: IUser,
): Promise<FeatureToggle> {
const ft =
await this.featureStrategiesStore.getFeatureToggleWithVariantEnvs(
Expand All @@ -2082,7 +2081,7 @@ class FeatureToggleService {
project: string,
environment: string,
newVariants: Operation[],
user: User,
user: IUser,
): Promise<IVariant[]> {
const oldVariants = await this.getVariantsForEnv(
featureName,
Expand Down Expand Up @@ -2136,7 +2135,7 @@ class FeatureToggleService {
featureName: string,
environment: string,
newVariants: IVariant[],
user: User,
user: IUser,
oldVariants?: IVariant[],
): Promise<IVariant[]> {
await variantsArraySchema.validateAsync(newVariants);
Expand Down Expand Up @@ -2174,7 +2173,7 @@ class FeatureToggleService {
featureName: string,
environment: string,
newVariants: IVariant[],
user: User,
user: IUser,
oldVariants?: IVariant[],
): Promise<IVariant[]> {
await this.stopWhenChangeRequestsEnabled(projectId, environment, user);
Expand All @@ -2193,7 +2192,7 @@ class FeatureToggleService {
featureName: string,
environments: string[],
newVariants: IVariant[],
user: User,
user: IUser,
): Promise<IVariant[]> {
for (const env of environments) {
await this.stopWhenChangeRequestsEnabled(projectId, env);
Expand All @@ -2212,7 +2211,7 @@ class FeatureToggleService {
featureName: string,
environments: string[],
newVariants: IVariant[],
user: User,
user: IUser,
): Promise<IVariant[]> {
await variantsArraySchema.validateAsync(newVariants);
const fixedVariants = this.fixVariantWeights(newVariants);
Expand Down Expand Up @@ -2290,7 +2289,7 @@ class FeatureToggleService {
private async stopWhenChangeRequestsEnabled(
project: string,
environment?: string,
user?: User,
user?: IUser,
) {
const canBypass = environment
? await this.changeRequestAccessReadModel.canBypassChangeRequest(
Expand All @@ -2311,7 +2310,7 @@ class FeatureToggleService {
project: string,
environment: string,
featureName: string,
user?: User,
user?: IUser,
) {
const hasEnvironment =
await this.featureEnvironmentStore.featureHasEnvironment(
Expand Down
Loading

0 comments on commit f16ad4e

Please sign in to comment.