Skip to content

Commit d594c4e

Browse files
CLN: Interface for User and UserCache Services (#68)
1 parent b69e76a commit d594c4e

24 files changed

+219
-113
lines changed

src/authentication/authentication.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const providers: Provider[] = [
7575
TwilioImplModule,
7676
HttpModule,
7777
],
78-
providers: providers,
78+
providers,
7979
controllers: [GoogleAuthController],
8080
})
8181
export class UserAuthModule {}

src/authentication/service/google.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Inject, Injectable } from '@nestjs/common';
22
import { InvalidPayloadException } from '../exception/userauth.exception';
33
import User from '../../authorization/entity/user.entity';
44
import { GoogleUserSchema } from '../validation/user.auth.schema.validation';
5-
import UserService from '../../authorization/service/user.service';
5+
import { UserServiceInterface } from '../../authorization/service/user.service.interface';
66
import { AuthenticationHelper } from '../authentication.helper';
77
import { GoogleLoginUser } from '../passport/googleStrategy';
88

99
@Injectable()
1010
export class GoogleAuthService {
1111
constructor(
12-
private userService: UserService,
12+
@Inject(UserServiceInterface) private userService: UserServiceInterface,
1313
private authenticationHelper: AuthenticationHelper,
1414
) {}
1515
private async validateInput(

src/authentication/service/otp.auth.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Inject, Injectable } from '@nestjs/common';
22
import User from '../../authorization/entity/user.entity';
33
import { UserNotFoundException } from '../../authorization/exception/user.exception';
4-
import UserService from '../../authorization/service/user.service';
4+
import { UserServiceInterface } from '../../authorization/service/user.service.interface';
55
import {
66
Status,
77
TokenResponse,
@@ -20,7 +20,7 @@ import { TokenService } from './token.service';
2020
@Injectable()
2121
export default class OTPAuthService implements Authenticatable {
2222
constructor(
23-
private userService: UserService,
23+
@Inject(UserServiceInterface) private userService: UserServiceInterface,
2424
private tokenService: TokenService,
2525
private otpService: OTPVerifiable,
2626
) {}

src/authentication/service/password.auth.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Inject, Injectable } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import { DataSource } from 'typeorm';
44
import User from '../../authorization/entity/user.entity';
55
import {
66
PasswordAlreadySetException,
77
UserNotFoundException,
88
} from '../../authorization/exception/user.exception';
9-
import UserService from '../../authorization/service/user.service';
9+
import { UserServiceInterface } from '../../authorization/service/user.service.interface';
1010
import {
1111
InviteTokenResponse,
1212
Status,
@@ -29,7 +29,7 @@ import { TokenService } from './token.service';
2929
@Injectable()
3030
export default class PasswordAuthService implements Authenticatable {
3131
constructor(
32-
private userService: UserService,
32+
@Inject(UserServiceInterface) private userService: UserServiceInterface,
3333
private tokenService: TokenService,
3434
private authenticationHelper: AuthenticationHelper,
3535
private dataSource: DataSource,

src/authentication/service/token.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Injectable, UnauthorizedException } from '@nestjs/common';
1+
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
22
import { ConfigService } from '@nestjs/config';
33
import User from '../../authorization/entity/user.entity';
44
import {
55
InviteTokenAlreadyRevokedException,
66
PasswordAlreadySetException,
77
} from '../../authorization/exception/user.exception';
8-
import UserService from '../../authorization/service/user.service';
8+
import { UserServiceInterface } from '../../authorization/service/user.service.interface';
99
import {
1010
InviteTokenResponse,
1111
TokenResponse,
@@ -15,7 +15,7 @@ import { AuthenticationHelper } from '../authentication.helper';
1515
@Injectable()
1616
export class TokenService {
1717
constructor(
18-
private userService: UserService,
18+
@Inject(UserServiceInterface) private userService: UserServiceInterface,
1919
private authenticationHelper: AuthenticationHelper,
2020
private configService: ConfigService,
2121
) {}

src/authorization/authorization.guard.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
1+
import {
2+
CanActivate,
3+
ExecutionContext,
4+
Inject,
5+
Injectable,
6+
} from '@nestjs/common';
27
import { Reflector } from '@nestjs/core';
38
import { GqlExecutionContext } from '@nestjs/graphql';
4-
import UserService from './service/user.service';
9+
import { UserServiceInterface } from './service/user.service.interface';
510

611
@Injectable()
712
export class AuthorizationGaurd implements CanActivate {
8-
constructor(private userService: UserService, private reflector: Reflector) {}
13+
constructor(
14+
@Inject(UserServiceInterface) private userService: UserServiceInterface,
15+
private reflector: Reflector,
16+
) {}
917
public async canActivate(context: ExecutionContext): Promise<boolean> {
1018
const ctx = GqlExecutionContext.create(context).getContext();
1119
const permissionsRequired = this.reflector.get<string[]>(

src/authorization/authorization.module.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ import { RoleServiceInterface } from './service/role.service.interface';
4646
import RoleCacheService from './service/rolecache.service';
4747
import { RoleCacheServiceInterface } from './service/rolecache.service.interface';
4848
import SearchService from './service/search.service';
49-
import UserService from './service/user.service';
50-
import UserCacheService from './service/usercache.service';
49+
import { UserService } from './service/user.service';
50+
import { UserServiceInterface } from './service/user.service.interface';
51+
import { UserCacheService } from './service/usercache.service';
52+
import { UserCacheServiceInterface } from './service/usercache.service.interface';
5153

5254
@Module({
5355
imports: [
@@ -69,11 +71,9 @@ import UserCacheService from './service/usercache.service';
6971
providers: [
7072
GroupResolver,
7173
PermissionResolver,
72-
UserService,
7374
UserResolver,
7475
EntityResolver,
7576
RedisCacheService,
76-
UserCacheService,
7777
AuthenticationHelper,
7878
ConfigService,
7979
RoleResolver,
@@ -117,7 +117,20 @@ import UserCacheService from './service/usercache.service';
117117
provide: GroupCacheServiceInterface,
118118
useClass: GroupCacheService,
119119
},
120+
{
121+
provide: UserServiceInterface,
122+
useClass: UserService,
123+
},
124+
{
125+
provide: UserCacheServiceInterface,
126+
useClass: UserCacheService,
127+
},
128+
],
129+
exports: [
130+
{
131+
provide: UserServiceInterface,
132+
useClass: UserService,
133+
},
120134
],
121-
exports: [UserService],
122135
})
123136
export class AuthorizationModule {}

src/authorization/resolver/user.resolver.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UseGuards } from '@nestjs/common';
1+
import { Inject, UseGuards } from '@nestjs/common';
22
import { ParseUUIDPipe } from '@nestjs/common/pipes';
33
import {
44
Args,
@@ -24,12 +24,14 @@ import {
2424
import ValidationPipe from '../../validation/validation.pipe';
2525
import { PermissionsType } from '../constants/authorization.constants';
2626
import { Permissions } from '../permissions.decorator';
27-
import UserService from '../service/user.service';
27+
import { UserServiceInterface } from '../service/user.service.interface';
2828
import * as UserSchema from '../validation/user.validation.schema';
2929

3030
@Resolver('User')
3131
export class UserResolver {
32-
constructor(private userService: UserService) {}
32+
constructor(
33+
@Inject(UserServiceInterface) private userService: UserServiceInterface,
34+
) {}
3335

3436
@Permissions(PermissionsType.ViewUser)
3537
@Query()

src/authorization/service/group.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { UserGroupRepository } from '../repository/userGroup.repository';
3232
import { GroupServiceInterface } from './group.service.interface';
3333
import { GroupCacheServiceInterface } from './groupcache.service.interface';
3434
import SearchService from './search.service';
35-
import UserCacheService from './usercache.service';
35+
import { UserCacheServiceInterface } from './usercache.service.interface';
3636

3737
@Injectable()
3838
export class GroupService implements GroupServiceInterface {
@@ -47,7 +47,8 @@ export class GroupService implements GroupServiceInterface {
4747
private dataSource: DataSource,
4848
@Inject(GroupCacheServiceInterface)
4949
private groupCacheService: GroupCacheServiceInterface,
50-
private userCacheService: UserCacheService,
50+
@Inject(UserCacheServiceInterface)
51+
private userCacheService: UserCacheServiceInterface,
5152
private searchService: SearchService,
5253
) {}
5354

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import User from '../entity/user.entity';
2+
import {
3+
OperationType,
4+
UpdateUserGroupInput,
5+
UpdateUserInput,
6+
UpdateUserPermissionInput,
7+
UserInputFilter,
8+
} from '../../schema/graphql.schema';
9+
import Group from '../entity/group.entity';
10+
import Permission from '../entity/permission.entity';
11+
12+
export interface UserServiceInterface {
13+
getAllUsers(input?: UserInputFilter): Promise<[User[], number]>;
14+
15+
getUserById(id: string): Promise<User>;
16+
17+
createUser(user: User): Promise<User>;
18+
19+
updateUser(id: string, user: UpdateUserInput): Promise<User>;
20+
21+
updateUserGroups(id: string, user: UpdateUserGroupInput): Promise<Group[]>;
22+
23+
getUserGroups(id: string): Promise<Group[]>;
24+
25+
updateUserPermissions(
26+
id: string,
27+
request: UpdateUserPermissionInput,
28+
): Promise<Permission[]>;
29+
30+
getUserPermissions(id: string): Promise<Permission[]>;
31+
32+
deleteUser(id: string): Promise<User>;
33+
34+
getAllUserpermissionIds(id: string): Promise<Set<string>>;
35+
36+
permissionsOfUser(id: string): Promise<Permission[]>;
37+
38+
verifyUserPermissions(
39+
id: string,
40+
permissionsToVerify: string[],
41+
operation?: OperationType,
42+
): Promise<boolean>;
43+
44+
verifyDuplicateUser(
45+
email?: string | undefined,
46+
phone?: string | undefined,
47+
): Promise<{ existingUserDetails?: User | null; duplicate: string }>;
48+
49+
getUserDetailsByEmailOrPhone(
50+
email?: string | undefined,
51+
phone?: string | undefined,
52+
): Promise<any>;
53+
54+
getUserDetailsByUsername(
55+
email?: string | undefined,
56+
phone?: string | undefined,
57+
): Promise<User | null>;
58+
59+
updateField(id: string, field: string, value: any): Promise<User>;
60+
61+
getActiveUserByPhoneNumber(phone: string): Promise<User | null>;
62+
63+
setOtpSecret(user: User, twoFASecret: string): Promise<void>;
64+
}
65+
66+
export const UserServiceInterface = Symbol('UserServiceInterface');

0 commit comments

Comments
 (0)