Skip to content

Commit 9d5a7fa

Browse files
committed
Migrate remaining session decorations to ALS service
1 parent 30402ce commit 9d5a7fa

File tree

12 files changed

+52
-73
lines changed

12 files changed

+52
-73
lines changed

src/components/authentication/extra-info.resolver.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { ResolveField, Resolver } from '@nestjs/graphql';
22
import { mapValues } from '@seedcompany/common';
3-
import {
4-
type AbstractClassType,
5-
AnonSession,
6-
EnhancedResource,
7-
type Session,
8-
} from '~/common';
3+
import { type AbstractClassType, EnhancedResource } from '~/common';
94
import { Privileges } from '../authorization';
105
import { BetaFeatures } from '../authorization/dto/beta-features.dto';
116
import { LoginOutput, RegisterOutput, SessionOutput } from './dto';
@@ -16,7 +11,7 @@ function AuthExtraInfoResolver(concreteClass: AbstractClassType<any>) {
1611
constructor(private readonly privileges: Privileges) {}
1712

1813
@ResolveField(() => BetaFeatures)
19-
betaFeatures(@AnonSession() session: Session): BetaFeatures {
14+
betaFeatures(): BetaFeatures {
2015
const privileges = this.privileges.for(BetaFeatures);
2116
const { props } = EnhancedResource.of(BetaFeatures);
2217
return mapValues.fromList([...props], (prop) =>

src/components/authentication/login.resolver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Resolver,
77
} from '@nestjs/graphql';
88
import { stripIndent } from 'common-tags';
9-
import { AnonSession, type Session } from '~/common';
109
import { Loader, type LoaderOf } from '~/core';
1110
import { Privileges } from '../authorization';
1211
import { Power } from '../authorization/dto';
@@ -15,11 +14,13 @@ import { User } from '../user/dto';
1514
import { Anonymous } from './anonymous.decorator';
1615
import { AuthenticationService } from './authentication.service';
1716
import { LoginInput, LoginOutput, LogoutOutput } from './dto';
17+
import { SessionHost } from './session.host';
1818

1919
@Resolver(LoginOutput)
2020
export class LoginResolver {
2121
constructor(
2222
private readonly authentication: AuthenticationService,
23+
private readonly sessionHost: SessionHost,
2324
private readonly privileges: Privileges,
2425
) {}
2526

@@ -43,7 +44,8 @@ export class LoginResolver {
4344
`,
4445
})
4546
@Anonymous()
46-
async logout(@AnonSession() session: Session): Promise<LogoutOutput> {
47+
async logout(): Promise<LogoutOutput> {
48+
const session = this.sessionHost.current;
4749
await this.authentication.logout(session.token);
4850
await this.authentication.refreshCurrentSession(); // ensure session data is fresh
4951
return { success: true };

src/components/changeset/changeset-aware.resolver.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import { Info, Parent, ResolveField, Resolver } from '@nestjs/graphql';
22
import { stripIndent } from 'common-tags';
3-
import {
4-
AnonSession,
5-
Fields,
6-
IsOnlyId,
7-
Resource,
8-
type Session,
9-
} from '~/common';
3+
import { Fields, IsOnlyId, Resource } from '~/common';
104
import { ResourceLoader, ResourceResolver } from '~/core';
5+
import { Identity } from '~/core/authentication';
116
import { ChangesetResolver } from './changeset.resolver';
127
import { Changeset, ChangesetAware, ChangesetDiff } from './dto';
138

149
@Resolver(ChangesetAware)
1510
export class ChangesetAwareResolver {
1611
constructor(
1712
private readonly resources: ResourceLoader,
13+
private readonly identity: Identity,
1814
private readonly resourceResolver: ResourceResolver,
1915
private readonly changesetResolver: ChangesetResolver,
2016
) {}
@@ -55,22 +51,17 @@ export class ChangesetAwareResolver {
5551
})
5652
async changesetDiff(
5753
@Parent() object: ChangesetAware,
58-
@AnonSession() session: Session,
5954
): Promise<ChangesetDiff | null> {
6055
// TODO move to auth policy
61-
if (session.anonymous) {
56+
if (this.identity.isAnonymous) {
6257
return null;
6358
}
6459

6560
const changeset = await this.changeset(object);
6661
if (!changeset) {
6762
return null;
6863
}
69-
const diff = await this.changesetResolver.difference(
70-
changeset,
71-
session,
72-
object.id,
73-
);
64+
const diff = await this.changesetResolver.difference(changeset, object.id);
7465
return diff;
7566
}
7667
}

src/components/changeset/changeset.resolver.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
2-
import {
3-
AnonSession,
4-
type ID,
5-
IdArg,
6-
type ObjectView,
7-
type Session,
8-
} from '~/common';
2+
import { type ID, IdArg, type ObjectView } from '~/common';
93
import { ResourceLoader } from '~/core';
4+
import { Identity } from '~/core/authentication';
105
import { type BaseNode } from '~/core/database/results';
116
import { ChangesetRepository } from './changeset.repository';
127
import { Changeset, ChangesetDiff, type ResourceChange } from './dto';
@@ -16,6 +11,7 @@ export class ChangesetResolver {
1611
constructor(
1712
private readonly repo: ChangesetRepository,
1813
private readonly resources: ResourceLoader,
14+
private readonly identity: Identity,
1915
) {}
2016

2117
@Query(() => Changeset)
@@ -28,7 +24,6 @@ export class ChangesetResolver {
2824
})
2925
async difference(
3026
@Parent() changeset: Changeset,
31-
@AnonSession() session: Session,
3227
@IdArg({
3328
name: 'resource',
3429
nullable: true,
@@ -38,7 +33,7 @@ export class ChangesetResolver {
3833
parent?: ID,
3934
): Promise<ChangesetDiff> {
4035
// TODO move to auth policy
41-
if (session.anonymous) {
36+
if (this.identity.isAnonymous) {
4237
return { added: [], removed: [], changed: [] };
4338
}
4439

src/components/comments/commentable.resolver.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
import { Info, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
22
import {
3-
AnonSession,
43
Fields,
54
type ID,
65
IdArg,
76
IsOnly,
87
ListArg,
98
type Resource,
109
SecuredList,
11-
type Session,
1210
} from '~/common';
1311
import { Loader, type LoaderOf } from '~/core';
12+
import { Identity } from '~/core/authentication';
1413
import { CommentThreadLoader } from './comment-thread.loader';
1514
import { CommentService } from './comment.service';
1615
import { Commentable, CommentThreadList, CommentThreadListInput } from './dto';
1716

1817
@Resolver(Commentable)
1918
export class CommentableResolver {
20-
constructor(private readonly service: CommentService) {}
19+
constructor(
20+
private readonly service: CommentService,
21+
private readonly identity: Identity,
22+
) {}
2123

2224
@Query(() => Commentable, {
2325
description: 'Load a commentable resource by ID',
@@ -32,12 +34,11 @@ export class CommentableResolver {
3234
async commentThreads(
3335
@Parent() parent: Commentable & Resource,
3436
@ListArg(CommentThreadListInput) input: CommentThreadListInput,
35-
@AnonSession() session: Session,
3637
@Loader(CommentThreadLoader) commentThreads: LoaderOf<CommentThreadLoader>,
3738
@Info(Fields, IsOnly(['total'])) onlyTotal: boolean,
3839
) {
3940
// TODO move to auth policy
40-
if (session.anonymous) {
41+
if (this.identity.isAnonymous) {
4142
return { parent, ...SecuredList.Redacted };
4243
}
4344
if (onlyTotal) {

src/components/file/file-node.resolver.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
22
import { stripIndent } from 'common-tags';
3-
import { AnonSession, type Session } from '~/common';
43
import { Loader, type LoaderOf } from '~/core';
54
import { UserLoader } from '../user';
65
import { User } from '../user/dto';
@@ -33,10 +32,7 @@ export class FileNodeResolver {
3332
without having to fetch each parent serially.
3433
`,
3534
})
36-
async parents(
37-
@Parent() node: FileNode,
38-
@AnonSession() _session: Session,
39-
): Promise<readonly FileNode[]> {
35+
async parents(@Parent() node: FileNode): Promise<readonly FileNode[]> {
4036
return await this.service.getParents(node.id);
4137
}
4238

src/components/notifications/notification.resolver.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
2-
import { AnonSession, ListArg, type Session } from '~/common';
2+
import { ListArg } from '~/common';
3+
import { Identity } from '~/core/authentication';
34
import {
45
MarkNotificationReadArgs,
56
Notification,
@@ -10,15 +11,17 @@ import { NotificationServiceImpl } from './notification.service';
1011

1112
@Resolver()
1213
export class NotificationResolver {
13-
constructor(private readonly service: NotificationServiceImpl) {}
14+
constructor(
15+
private readonly service: NotificationServiceImpl,
16+
private readonly identity: Identity,
17+
) {}
1418

1519
@Query(() => NotificationList)
1620
async notifications(
17-
@AnonSession() session: Session,
1821
@ListArg(NotificationListInput) input: NotificationListInput,
1922
): Promise<NotificationList> {
2023
// TODO move to DB layer?
21-
if (session.anonymous) {
24+
if (this.identity.isAnonymous) {
2225
return { items: [], total: 0, totalUnread: 0, hasMore: false };
2326
}
2427
return await this.service.list(input);

src/components/pin/pin.resolver.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
2-
import {
3-
AnonSession,
4-
type ID,
5-
IdArg,
6-
ListArg,
7-
NotImplementedException,
8-
type Session,
9-
} from '~/common';
2+
import { type ID, IdArg, ListArg, NotImplementedException } from '~/common';
3+
import { Identity } from '~/core/authentication';
104
import { PinnedListInput, type PinnedListOutput } from './dto';
115
import { PinService } from './pin.service';
126

137
@Resolver()
148
export class PinResolver {
15-
constructor(readonly pins: PinService) {}
9+
constructor(
10+
private readonly pins: PinService,
11+
private readonly identity: Identity,
12+
) {}
1613

1714
@Query(() => Boolean, {
1815
description:
1916
'Returns whether or not the requesting user has pinned the resource ID',
2017
})
2118
async isPinned(
22-
@AnonSession() session: Session,
2319
@IdArg({
2420
description: 'A resource ID',
2521
})
2622
id: ID,
2723
): Promise<boolean> {
2824
// TODO move to DB layer?
29-
if (session.anonymous) {
25+
if (this.identity.isAnonymous) {
3026
return false;
3127
}
3228
return await this.pins.isPinned(id);

src/components/product-progress/create-product-connection.resolver.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
2-
import { AnonSession, type Session, Variant } from '~/common';
2+
import { Variant } from '~/common';
3+
import { Identity } from '~/core/authentication';
34
import { CreateProductOutput } from '../product/dto';
45
import { ProductProgressService } from './product-progress.service';
56

67
@Resolver(() => CreateProductOutput)
78
export class ProgressReportCreateProductConnectionResolver {
8-
constructor(private readonly service: ProductProgressService) {}
9+
constructor(
10+
private readonly service: ProductProgressService,
11+
private readonly identity: Identity,
12+
) {}
913

1014
@ResolveField(() => [Variant], {
1115
description: 'All available progress variants for this product',
1216
})
1317
async availableVariants(
1418
@Parent() { product }: CreateProductOutput,
15-
@AnonSession() session: Session,
1619
): Promise<readonly Variant[]> {
1720
// TODO move to auth policy
18-
if (session.anonymous) {
21+
if (this.identity.isAnonymous) {
1922
return [];
2023
}
2124
return await this.service.getAvailableVariantsForProduct(product);

src/components/project/financial-approver/financial-approver.resolver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
ResolveField,
77
Resolver,
88
} from '@nestjs/graphql';
9-
import { AnonSession, type Session } from '~/common';
109
import { Loader, type LoaderOf } from '~/core';
10+
import { Identity } from '~/core/authentication';
1111
import { Privileges } from '../../authorization';
1212
import { UserLoader } from '../../user';
1313
import { User } from '../../user/dto';
@@ -20,6 +20,7 @@ export class FinancialApproverResolver {
2020
constructor(
2121
private readonly repo: FinancialApproverRepository,
2222
private readonly privileges: Privileges,
23+
private readonly identity: Identity,
2324
) {}
2425

2526
@Query(() => [FinancialApprover])
@@ -30,10 +31,9 @@ export class FinancialApproverResolver {
3031
nullable: true,
3132
})
3233
types: readonly ProjectType[] | undefined,
33-
@AnonSession() session: Session,
3434
): Promise<readonly FinancialApprover[]> {
3535
// TODO move to auth policy
36-
if (session.anonymous) {
36+
if (this.identity.isAnonymous) {
3737
return [];
3838
}
3939
return await this.repo.read(types);

0 commit comments

Comments
 (0)