Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Dec 17, 2024
2 parents 564b9f1 + 966b2db commit 7661b35
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/components/authentication/authentication.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { SessionResolver } from './session.resolver';
RegisterExtraInfoResolver,
AuthenticationService,
splitDb(AuthenticationRepository, AuthenticationEdgeDBRepository),
{ provide: 'AUTHENTICATION', useExisting: AuthenticationService },
CryptoService,
SessionInterceptor,
{ provide: APP_INTERCEPTOR, useExisting: SessionInterceptor },
Expand All @@ -44,6 +45,7 @@ import { SessionResolver } from './session.resolver';
exports: [
SessionInterceptor,
AuthenticationService,
'AUTHENTICATION',
CryptoService,
AuthenticationRepository,
],
Expand Down
10 changes: 9 additions & 1 deletion src/components/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { CachedByArg } from '@seedcompany/common';
import { EmailService } from '@seedcompany/nestjs-email';
import JWT from 'jsonwebtoken';
import { DateTime } from 'luxon';
Expand Down Expand Up @@ -179,8 +180,9 @@ export class AuthenticationService {
return session;
}

@CachedByArg()
lazySessionForRootUser(input?: Partial<Session>) {
const promiseOfRootId = this.repo.waitForRootUserId().then((id) => {
const promiseOfRootId = this.waitForRootUserIdOnce().then((id) => {
(session as Writable<Session>).userId = id;
return id;
});
Expand Down Expand Up @@ -216,6 +218,12 @@ export class AuthenticationService {
},
}) as LazySession;
}

@CachedByArg()
private waitForRootUserIdOnce() {
return this.repo.waitForRootUserId();
}

async sessionForUser(userId: ID): Promise<Session> {
const roles = await this.repo.rolesForUser(userId);
const session: Session = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FileVersion } from '../../file/dto';
import { PeriodicReportUploadedEvent } from '../../periodic-report/events';
import { ProgressReport } from '../dto';

@Migration('2024-12-16T13:00:00')
@Migration('2024-12-16T16:33:00')
export class ReextractPnpProgressReportsMigration extends BaseMigration {
constructor(
private readonly eventBus: IEventBus,
Expand Down
4 changes: 4 additions & 0 deletions src/core/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export const makeConfig = (env: EnvironmentService) =>

/** Is this a REPL process? */
isRepl = process.argv.join(' ').includes('repl');
/** Is this a console command process? */
isConsole = process.argv.join(' ').includes('console');
/** Is this a REPL / console command process? */
isCli = this.isRepl || this.isConsole;

/** Is this a jest process? */
jest = Boolean(env.string('JEST_WORKER_ID').optional());
Expand Down
4 changes: 4 additions & 0 deletions src/core/data-loader/data-loader.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Injectable } from '@nestjs/common';
import { DataLoaderOptions } from '@seedcompany/data-loader';
import { NotFoundException } from '~/common';
import { ConfigService } from '../config/config.service';

@Injectable()
export class DataLoaderConfig {
constructor(private readonly config: ConfigService) {}

create(): DataLoaderOptions<any, any> {
return {
// Increase the batching timeframe from the same nodejs frame to 10ms
Expand All @@ -13,6 +16,7 @@ export class DataLoaderConfig {
new NotFoundException(
`Could not find ${String(typeName)} (${String(cacheKey)})`,
),
cache: !this.config.isCli,
};
}
}
16 changes: 14 additions & 2 deletions src/core/data-loader/session-aware-loader.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Inject } from '@nestjs/common';
import { DataLoaderStrategy } from '@seedcompany/data-loader';
import { ID } from '~/common';
import { ID, Session } from '~/common';
import { sessionFromContext } from '~/common/session';
import type { AuthenticationService } from '../../components/authentication';
import { ConfigService } from '../config/config.service';
import { GqlContextHost } from '../graphql';

export abstract class SessionAwareLoaderStrategy<T, Key = ID, CachedKey = Key>
Expand All @@ -14,7 +16,17 @@ export abstract class SessionAwareLoaderStrategy<T, Key = ID, CachedKey = Key>
@Inject(GqlContextHost)
private readonly contextHost: GqlContextHost;

get session() {
@Inject(ConfigService)
private readonly config: ConfigService;

@Inject('AUTHENTICATION')
private readonly auth: AuthenticationService & {};

get session(): Session {
if (this.config.isCli) {
return this.auth.lazySessionForRootUser();
}

return sessionFromContext(this.contextHost.context);
}
}
14 changes: 12 additions & 2 deletions src/core/resources/resource.loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Injectable, Type } from '@nestjs/common';
import { Inject, Injectable, Type } from '@nestjs/common';
import {
DataLoaderContext,
DataLoaderStrategy,
} from '@seedcompany/data-loader';
import { ConditionalKeys, Merge, ValueOf } from 'type-fest';
import { ID, Many, ObjectView, ServerException } from '~/common';
import type { AuthenticationService } from '../../components/authentication';
import { ConfigService } from '../config/config.service';
import { BaseNode } from '../database/results';
import { GqlContextHost } from '../graphql';
import { ResourceLoaderRegistry } from './loader.registry';
Expand Down Expand Up @@ -41,6 +43,8 @@ export class ResourceLoader {
constructor(
private readonly loaderRegistry: ResourceLoaderRegistry,
private readonly contextHost: GqlContextHost,
private readonly config: ConfigService,
@Inject('AUTHENTICATION') private readonly auth: AuthenticationService & {},
private readonly loaderContext: DataLoaderContext,
private readonly resourceResolver: ResourceResolver,
) {}
Expand Down Expand Up @@ -106,9 +110,13 @@ export class ResourceLoader {
async getLoader<T, Key, CachedKey = Key>(
type: Type<DataLoaderStrategy<T, Key, CachedKey>>,
) {
if (this.config.isCli) {
// Ensure the default root session is ready to go for data loaders
await this.auth.lazySessionForRootUser();
}
return await this.loaderContext.getLoader<T, Key, CachedKey>(
type,
this.contextHost.context,
this.config.isCli ? CLI_CONTEXT_ID : this.contextHost.context,
);
}

Expand All @@ -134,3 +142,5 @@ export class ResourceLoader {
return { resolvedType, ...found };
}
}

const CLI_CONTEXT_ID = {};

0 comments on commit 7661b35

Please sign in to comment.