From ef16f38a6763604c343c42510523996ce32d68fa Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 21 Aug 2025 12:05:19 +0200 Subject: [PATCH] refactor --- package-lock.json | 12 ++++---- package.json | 2 +- src/contracts/index.ts | 12 -------- src/contracts/models/account.ts | 23 ++------------- src/contracts/models/group.ts | 19 ++++-------- src/contracts/models/organization.ts | 7 +---- src/contracts/models/registration.ts | 17 +---------- src/contracts/models/right.ts | 6 +--- src/contracts/models/session.ts | 11 ++----- src/contracts/models/sign.in.ts | 6 +--- .../services/mail.verification.service.ts | 6 ++-- src/contracts/services/password.service.ts | 2 +- src/contracts/services/session.service.ts | 12 ++++---- src/contracts/services/token.service.ts | 10 +++---- src/guards/index.ts | 4 --- src/guards/organization.decorator.ts | 4 +-- src/guards/rights.decorator.ts | 4 +-- src/guards/roles.decorator.ts | 4 +-- src/guards/token.guard.ts | 26 ++++++++--------- src/index.ts | 4 +-- src/models/account.ts | 29 +++++++++++++++++++ src/models/group.ts | 19 ++++++++++++ src/models/organization.ts | 10 +++++++ src/models/registration.ts | 19 ++++++++++++ src/models/right.ts | 9 ++++++ src/models/session.ts | 13 +++++++++ src/models/sign.in.ts | 10 +++++++ src/services/account.factory.service.ts | 13 +++++---- src/services/account.service.ts | 22 +++++++------- src/services/group.factory.service.ts | 5 ++-- src/services/group.service.ts | 16 +++++----- src/services/index.ts | 10 ------- src/services/login.service.ts | 13 +++++---- src/services/organization.factory.service.ts | 7 +++-- src/services/organization.service.ts | 18 ++++++------ src/services/registration.service.ts | 8 ++--- src/services/right.factory.service.ts | 5 ++-- src/services/right.service.ts | 16 +++++----- 38 files changed, 232 insertions(+), 201 deletions(-) delete mode 100644 src/contracts/index.ts delete mode 100644 src/guards/index.ts create mode 100644 src/models/account.ts create mode 100644 src/models/group.ts create mode 100644 src/models/organization.ts create mode 100644 src/models/registration.ts create mode 100644 src/models/right.ts create mode 100644 src/models/session.ts create mode 100644 src/models/sign.in.ts delete mode 100644 src/services/index.ts diff --git a/package-lock.json b/package-lock.json index 14be4af..68a61cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@apihub24/token-authentication", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@apihub24/token-authentication", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "dependencies": { - "@apihub24/repository": "^1.0.1", + "@apihub24/repository": "^1.0.2", "@nestjs/common": "^11.1.6", "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.1.6", @@ -20,9 +20,9 @@ } }, "node_modules/@apihub24/repository": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@apihub24/repository/-/repository-1.0.1.tgz", - "integrity": "sha512-ex3Z+lxsHtVKDTolJQqLHswq9SKfXzM/hWv17zsrhKqJwuGxO7CeBFM60aiuApZX9NqBhGAkPGGj9jt+F/Y9HQ==", + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@apihub24/repository/-/repository-1.0.2.tgz", + "integrity": "sha512-brZkSENCpC1/MJYBSHHeO+8odrKdq/q4U2z7HN5mrAvPl8GNjq0egOCZ22axQKynWuP9yimIIwJxExwp12YBzQ==", "license": "MIT" }, "node_modules/@borewit/text-codec": { diff --git a/package.json b/package.json index 98cbb90..d4f6315 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "@nestjs/common": "^11.1.6", "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.1.6", - "@apihub24/repository": "^1.0.1", + "@apihub24/repository": "^1.0.2", "class-validator": "^0.14.2" }, "devDependencies": { diff --git a/src/contracts/index.ts b/src/contracts/index.ts deleted file mode 100644 index 378a316..0000000 --- a/src/contracts/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -export * from "./models/account"; -export * from "./models/group"; -export * from "./models/organization"; -export * from "./models/registration"; -export * from "./models/right"; -export * from "./models/session"; -export * from "./models/sign.in"; - -export * from "./services/mail.verification.service"; -export * from "./services/password.service"; -export * from "./services/session.service"; -export * from "./services/token.service"; diff --git a/src/contracts/models/account.ts b/src/contracts/models/account.ts index 8562b5c..5cd007a 100644 --- a/src/contracts/models/account.ts +++ b/src/contracts/models/account.ts @@ -1,28 +1,11 @@ -import { Group } from './group'; -import { - IsEmail, - IsNotEmpty, - MinLength, - ValidateNested, -} from 'class-validator'; +import { IGroup } from "./group"; -export class Account { +export interface IAccount { id: string; - - @MinLength(3) accountName: string; - - @MinLength(3) passwordHash: string; - - @IsEmail() email: string; - emailVerified: boolean; - active: boolean; - - @IsNotEmpty({ each: true }) - @ValidateNested({ each: true }) - groups: Group[]; + groups: IGroup[]; } diff --git a/src/contracts/models/group.ts b/src/contracts/models/group.ts index 0f905ca..37bf32a 100644 --- a/src/contracts/models/group.ts +++ b/src/contracts/models/group.ts @@ -1,18 +1,9 @@ -import { Organization } from './organization'; -import { Right } from './right'; -import { IsNotEmpty, MinLength, ValidateNested } from 'class-validator'; +import { IOrganization } from "./organization"; +import { IRight } from "./right"; -export class Group { +export interface IGroup { id: string; - - @MinLength(3) name: string; - - @IsNotEmpty() - @ValidateNested() - organization: Organization; - - @IsNotEmpty({ each: true }) - @ValidateNested({ each: true }) - rights: Right[]; + organization: IOrganization; + rights: IRight[]; } diff --git a/src/contracts/models/organization.ts b/src/contracts/models/organization.ts index 49c2e3a..d91d0c6 100644 --- a/src/contracts/models/organization.ts +++ b/src/contracts/models/organization.ts @@ -1,9 +1,4 @@ -import { IsNotEmpty, MinLength } from 'class-validator'; - -export class Organization { +export interface IOrganization { id: string; - - @IsNotEmpty() - @MinLength(3) name: string; } diff --git a/src/contracts/models/registration.ts b/src/contracts/models/registration.ts index 00a0bf3..f185e38 100644 --- a/src/contracts/models/registration.ts +++ b/src/contracts/models/registration.ts @@ -1,22 +1,7 @@ -import { IsEmail, IsNotEmpty, MinLength } from 'class-validator'; - -export class Registration { - // @IsNotEmpty() - @MinLength(3) +export interface IRegistration { accountName: string; - - // @IsNotEmpty() - @MinLength(3) password: string; - - // @IsNotEmpty() - @MinLength(3) passwordComparer: string; - - // @IsNotEmpty() - @IsEmail() email: string; - - @IsNotEmpty({ each: true }) groupIds: string[]; } diff --git a/src/contracts/models/right.ts b/src/contracts/models/right.ts index 6b7f559..59bbb69 100644 --- a/src/contracts/models/right.ts +++ b/src/contracts/models/right.ts @@ -1,8 +1,4 @@ -import { MinLength } from 'class-validator'; - -export class Right { +export interface IRight { id: string; - - @MinLength(3) name: string; } diff --git a/src/contracts/models/session.ts b/src/contracts/models/session.ts index 61a1a8d..5ba12e4 100644 --- a/src/contracts/models/session.ts +++ b/src/contracts/models/session.ts @@ -1,12 +1,7 @@ -import { IsNotEmpty, ValidateNested } from "class-validator"; -import { Account } from "./account"; +import { IAccount } from "./account"; -export class Session { +export interface ISession { id: string; - - @IsNotEmpty() - @ValidateNested() - account: Account; - + account: IAccount; metaData: Record; } diff --git a/src/contracts/models/sign.in.ts b/src/contracts/models/sign.in.ts index 4946aec..3461bea 100644 --- a/src/contracts/models/sign.in.ts +++ b/src/contracts/models/sign.in.ts @@ -1,8 +1,4 @@ -import { MinLength } from 'class-validator'; - -export class SignIn { - @MinLength(3) +export interface ISignIn { accountName: string; - @MinLength(3) password: string; } diff --git a/src/contracts/services/mail.verification.service.ts b/src/contracts/services/mail.verification.service.ts index 7545056..004f2e1 100644 --- a/src/contracts/services/mail.verification.service.ts +++ b/src/contracts/services/mail.verification.service.ts @@ -1,6 +1,6 @@ -import { Account } from "../models/account"; +import { IAccount } from "../models/account"; -export interface MailVerificationService { - sendVerificationMail(account: Account): Promise; +export interface IMailVerificationService { + sendVerificationMail(account: IAccount): Promise; verify(email: string, code: string): Promise; } diff --git a/src/contracts/services/password.service.ts b/src/contracts/services/password.service.ts index 5d5c0ef..79545e9 100644 --- a/src/contracts/services/password.service.ts +++ b/src/contracts/services/password.service.ts @@ -1,4 +1,4 @@ -export interface PasswordService { +export interface IPasswordService { hash(plainTextPassword: string): Promise; verify(plainTextPassword: string, passwordHash: string): Promise; } diff --git a/src/contracts/services/session.service.ts b/src/contracts/services/session.service.ts index 9c53baa..33a986d 100644 --- a/src/contracts/services/session.service.ts +++ b/src/contracts/services/session.service.ts @@ -1,9 +1,9 @@ -import { Account } from "../models/account"; -import { Session } from "../models/session"; +import { IAccount } from "../models/account"; +import { ISession } from "../models/session"; -export interface SessionService { - create(account: Account): Promise; - getBy(filter: (account: Account) => boolean): Promise; - getById(sessionId: string): Promise; +export interface ISessionService { + create(account: IAccount): Promise; + getBy(filter: (account: IAccount) => boolean): Promise; + getById(sessionId: string): Promise; remove(sessionId: string): Promise; } diff --git a/src/contracts/services/token.service.ts b/src/contracts/services/token.service.ts index 5593c94..64b9cb6 100644 --- a/src/contracts/services/token.service.ts +++ b/src/contracts/services/token.service.ts @@ -1,8 +1,8 @@ -import { Account } from "../models/account"; -import { Session } from "../models/session"; +import { IAccount } from "../models/account"; +import { ISession } from "../models/session"; -export interface TokenService { - generate(session: Session): Promise; +export interface ITokenService { + generate(session: ISession): Promise; validate(token: string): Promise; - getAccount(token: string): Promise; + getAccount(token: string): Promise; } diff --git a/src/guards/index.ts b/src/guards/index.ts deleted file mode 100644 index 942b42b..0000000 --- a/src/guards/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from "./organization.decorator"; -export * from "./rights.decorator"; -export * from "./roles.decorator"; -export * from "./token.guard"; diff --git a/src/guards/organization.decorator.ts b/src/guards/organization.decorator.ts index 051e039..ccae042 100644 --- a/src/guards/organization.decorator.ts +++ b/src/guards/organization.decorator.ts @@ -1,5 +1,5 @@ -import { SetMetadata } from '@nestjs/common'; +import { SetMetadata } from "@nestjs/common"; -export const ORGANIZATIONS_KEY = 'organizations'; +export const ORGANIZATIONS_KEY = "organizations"; export const Organizations = (...organizations: string[]) => SetMetadata(ORGANIZATIONS_KEY, organizations); diff --git a/src/guards/rights.decorator.ts b/src/guards/rights.decorator.ts index 2f9f0a6..4604968 100644 --- a/src/guards/rights.decorator.ts +++ b/src/guards/rights.decorator.ts @@ -1,4 +1,4 @@ -import { SetMetadata } from '@nestjs/common'; +import { SetMetadata } from "@nestjs/common"; -export const RIGHTS_KEY = 'rights'; +export const RIGHTS_KEY = "rights"; export const Rights = (...rights: string[]) => SetMetadata(RIGHTS_KEY, rights); diff --git a/src/guards/roles.decorator.ts b/src/guards/roles.decorator.ts index e038e16..3d51da8 100644 --- a/src/guards/roles.decorator.ts +++ b/src/guards/roles.decorator.ts @@ -1,4 +1,4 @@ -import { SetMetadata } from '@nestjs/common'; +import { SetMetadata } from "@nestjs/common"; -export const ROLES_KEY = 'roles'; +export const ROLES_KEY = "roles"; export const Roles = (...roles: string[]) => SetMetadata(ROLES_KEY, roles); diff --git a/src/guards/token.guard.ts b/src/guards/token.guard.ts index 2b9b81f..5282bed 100644 --- a/src/guards/token.guard.ts +++ b/src/guards/token.guard.ts @@ -3,33 +3,33 @@ import { ExecutionContext, Inject, Injectable, -} from '@nestjs/common'; -import { Reflector } from '@nestjs/core'; -import { RIGHTS_KEY } from './rights.decorator'; -import { ROLES_KEY } from './roles.decorator'; -import * as tokenService from '../contracts/services/token.service'; -import { ORGANIZATIONS_KEY } from './organization.decorator'; +} from "@nestjs/common"; +import { Reflector } from "@nestjs/core"; +import { RIGHTS_KEY } from "./rights.decorator"; +import { ROLES_KEY } from "./roles.decorator"; +import * as tokenService from "../contracts/services/token.service"; +import { ORGANIZATIONS_KEY } from "./organization.decorator"; @Injectable() export class TokenGuard implements CanActivate { constructor( private reflector: Reflector, - @Inject('@apihub24/token_service') - private readonly tokenService: tokenService.TokenService, + @Inject("@apihub24/token_service") + private readonly tokenService: tokenService.ITokenService ) {} async canActivate(context: ExecutionContext): Promise { const requiredRights = this.reflector.getAllAndOverride( RIGHTS_KEY, - [context.getHandler(), context.getClass()], + [context.getHandler(), context.getClass()] ); const requiredRoles = this.reflector.getAllAndOverride( ROLES_KEY, - [context.getHandler(), context.getClass()], + [context.getHandler(), context.getClass()] ); const requiredOrganizations = this.reflector.getAllAndOverride( ORGANIZATIONS_KEY, - [context.getHandler(), context.getClass()], + [context.getHandler(), context.getClass()] ); if (!requiredRights && !requiredRoles && !requiredOrganizations) { return true; @@ -40,8 +40,8 @@ export class TokenGuard implements CanActivate { } = context.switchToHttp().getRequest(); if ( !authorization || - typeof authorization !== 'string' || - !authorization.includes('Bearer ') + typeof authorization !== "string" || + !authorization.includes("Bearer ") ) { return false; } diff --git a/src/index.ts b/src/index.ts index d8b0ce0..5931fa6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,3 @@ -export * from "./token-authentication.module"; - export * from "./contracts/models/account"; export * from "./contracts/models/group"; export * from "./contracts/models/organization"; @@ -28,3 +26,5 @@ export * from "./services/organization.service"; export * from "./services/registration.service"; export * from "./services/right.factory.service"; export * from "./services/right.service"; + +export * from "./token-authentication.module"; diff --git a/src/models/account.ts b/src/models/account.ts new file mode 100644 index 0000000..b166bce --- /dev/null +++ b/src/models/account.ts @@ -0,0 +1,29 @@ +import { IAccount } from "../contracts/models/account"; +import { Group } from "./group"; +import { + IsEmail, + IsNotEmpty, + MinLength, + ValidateNested, +} from "class-validator"; + +export class Account implements IAccount { + id: string; + + @MinLength(3) + accountName: string; + + @MinLength(3) + passwordHash: string; + + @IsEmail() + email: string; + + emailVerified: boolean; + + active: boolean; + + @IsNotEmpty({ each: true }) + @ValidateNested({ each: true }) + groups: Group[]; +} diff --git a/src/models/group.ts b/src/models/group.ts new file mode 100644 index 0000000..68575cc --- /dev/null +++ b/src/models/group.ts @@ -0,0 +1,19 @@ +import { IGroup } from "../contracts/models/group"; +import { Organization } from "./organization"; +import { Right } from "./right"; +import { IsNotEmpty, MinLength, ValidateNested } from "class-validator"; + +export class Group implements IGroup { + id: string; + + @MinLength(3) + name: string; + + @IsNotEmpty() + @ValidateNested() + organization: Organization; + + @IsNotEmpty({ each: true }) + @ValidateNested({ each: true }) + rights: Right[]; +} diff --git a/src/models/organization.ts b/src/models/organization.ts new file mode 100644 index 0000000..c652934 --- /dev/null +++ b/src/models/organization.ts @@ -0,0 +1,10 @@ +import { IsNotEmpty, MinLength } from "class-validator"; +import { IOrganization } from "../contracts/models/organization"; + +export class Organization implements IOrganization { + id: string; + + @IsNotEmpty() + @MinLength(3) + name: string; +} diff --git a/src/models/registration.ts b/src/models/registration.ts new file mode 100644 index 0000000..a3dbed6 --- /dev/null +++ b/src/models/registration.ts @@ -0,0 +1,19 @@ +import { IsEmail, IsNotEmpty, MinLength } from "class-validator"; +import { IRegistration } from "../contracts/models/registration"; + +export class Registration implements IRegistration { + @MinLength(3) + accountName: string; + + @MinLength(3) + password: string; + + @MinLength(3) + passwordComparer: string; + + @IsEmail() + email: string; + + @IsNotEmpty({ each: true }) + groupIds: string[]; +} diff --git a/src/models/right.ts b/src/models/right.ts new file mode 100644 index 0000000..40929f2 --- /dev/null +++ b/src/models/right.ts @@ -0,0 +1,9 @@ +import { MinLength } from "class-validator"; +import { IRight } from "../contracts/models/right"; + +export class Right implements IRight { + id: string; + + @MinLength(3) + name: string; +} diff --git a/src/models/session.ts b/src/models/session.ts new file mode 100644 index 0000000..9b87552 --- /dev/null +++ b/src/models/session.ts @@ -0,0 +1,13 @@ +import { IsNotEmpty, ValidateNested } from "class-validator"; +import { Account } from "./account"; +import { ISession } from "../contracts/models/session"; + +export class Session implements ISession { + id: string; + + @IsNotEmpty() + @ValidateNested() + account: Account; + + metaData: Record; +} diff --git a/src/models/sign.in.ts b/src/models/sign.in.ts new file mode 100644 index 0000000..817b70c --- /dev/null +++ b/src/models/sign.in.ts @@ -0,0 +1,10 @@ +import { MinLength } from "class-validator"; +import { ISignIn } from "../contracts/models/sign.in"; + +export class SignIn implements ISignIn { + @MinLength(3) + accountName: string; + + @MinLength(3) + password: string; +} diff --git a/src/services/account.factory.service.ts b/src/services/account.factory.service.ts index 2796712..d0ae877 100644 --- a/src/services/account.factory.service.ts +++ b/src/services/account.factory.service.ts @@ -1,20 +1,21 @@ import { Inject, Injectable } from "@nestjs/common"; import { validate } from "class-validator"; import { GroupService } from "./group.service"; -import * as contracts from "../contracts"; +import * as passwordService from "../contracts/services/password.service"; +import { IRegistration } from "../contracts/models/registration"; +import { IAccount } from "../contracts/models/account"; +import { Account } from "../models/account"; @Injectable() export class AccountFactoryService { constructor( @Inject("@apihub24/password_service") - private readonly passwordService: contracts.PasswordService, + private readonly passwordService: passwordService.IPasswordService, @Inject(GroupService) private readonly groupService: GroupService ) {} - async createFromRegistration( - registration: contracts.Registration - ): Promise { + async createFromRegistration(registration: IRegistration): Promise { let validationErrors = await validate(registration); if (validationErrors?.length) { throw new Error(validationErrors[0].toString()); @@ -22,7 +23,7 @@ export class AccountFactoryService { const groups = await this.groupService.getBy((x) => registration.groupIds.includes(x.id) ); - const account = new contracts.Account(); + const account = new Account(); account.accountName = registration.accountName; account.email = registration.email; account.emailVerified = false; diff --git a/src/services/account.service.ts b/src/services/account.service.ts index fe78941..551e6b8 100644 --- a/src/services/account.service.ts +++ b/src/services/account.service.ts @@ -1,34 +1,34 @@ -import { Account } from "../contracts/models/account"; -import { Inject, Injectable } from "@nestjs/common"; import * as repository from "@apihub24/repository"; -import { Group } from "../contracts"; +import { Inject, Injectable } from "@nestjs/common"; +import { IAccount } from "../contracts/models/account"; +import { IGroup } from "../contracts/models/group"; @Injectable() export class AccountService { constructor( @Inject("@apihub24/account_repository") - private readonly accountRepository: repository.Repository, + private readonly accountRepository: repository.IRepository, @Inject("@apihub24/group_repository") - private readonly groupRepository: repository.Repository + private readonly groupRepository: repository.IRepository ) {} - async getBy(filter: (account: Account) => boolean): Promise { + async getBy(filter: (account: IAccount) => boolean): Promise { return await this.accountRepository.getBy(filter); } - async save(account: Account): Promise { + async save(account: IAccount): Promise { const accounts = await this.accountRepository.save([account]); return accounts?.length ? accounts[0] : null; } - async delete(filter: (account: Account) => boolean): Promise { + async delete(filter: (account: IAccount) => boolean): Promise { return await this.accountRepository.deleteBy(filter); } async addAccountToGroup( accountId: string, groupId: string - ): Promise { + ): Promise { const [account, group] = await this.getAccountAndGroup(accountId, groupId); account.groups = account.groups.filter((x) => x.id !== groupId); account.groups.push(group); @@ -42,7 +42,7 @@ export class AccountService { async removeAccountFromGroup( accountId: string, groupId: string - ): Promise { + ): Promise { const [account] = await this.getAccountAndGroup(accountId, groupId); account.groups = account.groups.filter((x) => x.id !== groupId); const accountsSaved = await this.accountRepository.save([account]); @@ -55,7 +55,7 @@ export class AccountService { private async getAccountAndGroup( accountId: string, groupId: string - ): Promise<[Account, Group]> { + ): Promise<[IAccount, IGroup]> { const accounts = await this.accountRepository.getBy( (x) => x.id === accountId ); diff --git a/src/services/group.factory.service.ts b/src/services/group.factory.service.ts index 6b3091b..4f92c60 100644 --- a/src/services/group.factory.service.ts +++ b/src/services/group.factory.service.ts @@ -2,7 +2,8 @@ import { Inject, Injectable } from "@nestjs/common"; import { validate } from "class-validator"; import { OrganizationService } from "./organization.service"; import { RightService } from "./right.service"; -import { Group } from "../contracts"; +import { IGroup } from "../contracts/models/group"; +import { Group } from "../models/group"; @Injectable() export class GroupFactoryService { @@ -17,7 +18,7 @@ export class GroupFactoryService { name: string, organizationId: string, rightIds: string[] - ): Promise { + ): Promise { const rights = await this.rightService.getBy((x) => rightIds.includes(x.id) ); diff --git a/src/services/group.service.ts b/src/services/group.service.ts index 5668622..718522c 100644 --- a/src/services/group.service.ts +++ b/src/services/group.service.ts @@ -1,27 +1,27 @@ import { Inject, Injectable } from "@nestjs/common"; -import { Group } from "../contracts/models/group"; import * as repository from "@apihub24/repository"; -import { Right } from "../contracts"; +import { IGroup } from "../contracts/models/group"; +import { IRight } from "../contracts/models/right"; @Injectable() export class GroupService { constructor( @Inject("@apihub24/group_repository") - private readonly groupRepository: repository.Repository, + private readonly groupRepository: repository.IRepository, @Inject("@apihub24/right_repository") - private readonly rightRepository: repository.Repository + private readonly rightRepository: repository.IRepository ) {} - async getBy(filter: (group: Group) => boolean): Promise { + async getBy(filter: (group: IGroup) => boolean): Promise { return await this.groupRepository.getBy(filter); } - async save(group: Group): Promise { + async save(group: IGroup): Promise { const groups = await this.groupRepository.save([group]); return groups?.length ? groups[0] : null; } - async delete(filter: (group: Group) => boolean): Promise { + async delete(filter: (group: IGroup) => boolean): Promise { return this.groupRepository.deleteBy(filter); } @@ -49,7 +49,7 @@ export class GroupService { private async getGroupAndRight( groupId: string, rightId: string - ): Promise<[Group, Right]> { + ): Promise<[IGroup, IRight]> { const groups = await this.groupRepository.getBy((x) => x.id === groupId); if (!groups.length) { throw new Error(`group with id ${groupId} not found`); diff --git a/src/services/index.ts b/src/services/index.ts deleted file mode 100644 index 8a77039..0000000 --- a/src/services/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -export * from "./account.factory.service"; -export * from "./account.service"; -export * from "./group.factory.service"; -export * from "./group.service"; -export * from "./login.service"; -export * from "./organization.factory.service"; -export * from "./organization.service"; -export * from "./registration.service"; -export * from "./right.factory.service"; -export * from "./right.service"; diff --git a/src/services/login.service.ts b/src/services/login.service.ts index 8533bd3..310dfa8 100644 --- a/src/services/login.service.ts +++ b/src/services/login.service.ts @@ -1,21 +1,24 @@ import { Inject, Injectable } from "@nestjs/common"; -import * as contracts from "../contracts"; import { AccountService } from "./account.service"; +import * as tokenService from "../contracts/services/token.service"; +import * as passwordService from "../contracts/services/password.service"; +import * as sessionService from "../contracts/services/session.service"; +import { ISignIn } from "../contracts/models/sign.in"; @Injectable() export class LoginService { constructor( @Inject("@apihub24/token_service") - private readonly tokenService: contracts.TokenService, + private readonly tokenService: tokenService.ITokenService, @Inject("@apihub24/password_service") - private readonly passwordService: contracts.PasswordService, + private readonly passwordService: passwordService.IPasswordService, @Inject("@apihub24/session_service") - private readonly sessionService: contracts.SessionService, + private readonly sessionService: sessionService.ISessionService, @Inject(AccountService) private readonly accountService: AccountService ) {} - async signIn(signIn: contracts.SignIn): Promise { + async signIn(signIn: ISignIn): Promise { const accounts = await this.accountService.getBy( (x) => x.accountName === signIn.accountName && diff --git a/src/services/organization.factory.service.ts b/src/services/organization.factory.service.ts index e6a9146..3cf3ca0 100644 --- a/src/services/organization.factory.service.ts +++ b/src/services/organization.factory.service.ts @@ -1,9 +1,10 @@ -import { Injectable } from '@nestjs/common'; -import { Organization } from '../contracts/models/organization'; +import { Injectable } from "@nestjs/common"; +import { IOrganization } from "../contracts/models/organization"; +import { Organization } from "../models/organization"; @Injectable() export class OrganizationFactoryService { - createFromName(name: string): Organization { + createFromName(name: string): IOrganization { const organization = new Organization(); organization.name = name; return organization; diff --git a/src/services/organization.service.ts b/src/services/organization.service.ts index 049055e..95ee3a2 100644 --- a/src/services/organization.service.ts +++ b/src/services/organization.service.ts @@ -1,21 +1,21 @@ -import { Inject, Injectable } from '@nestjs/common'; -import * as repository from '@apihub24/repository'; -import { Organization } from '../contracts/models/organization'; +import { Inject, Injectable } from "@nestjs/common"; +import * as repository from "@apihub24/repository"; +import { IOrganization } from "src/contracts/models/organization"; @Injectable() export class OrganizationService { constructor( - @Inject('@apihub24/organization_repository') - private readonly organizationRepository: repository.Repository, + @Inject("@apihub24/organization_repository") + private readonly organizationRepository: repository.IRepository ) {} async getBy( - filter: (organization: Organization) => boolean, - ): Promise { + filter: (organization: IOrganization) => boolean + ): Promise { return await this.organizationRepository.getBy(filter); } - async save(organization: Organization): Promise { + async save(organization: IOrganization): Promise { const savedOrganizations = await this.organizationRepository.save([ organization, ]); @@ -26,7 +26,7 @@ export class OrganizationService { } async deleteBy( - filter: (organization: Organization) => boolean, + filter: (organization: IOrganization) => boolean ): Promise { return await this.organizationRepository.deleteBy(filter); } diff --git a/src/services/registration.service.ts b/src/services/registration.service.ts index 66c95e9..75ebda8 100644 --- a/src/services/registration.service.ts +++ b/src/services/registration.service.ts @@ -1,9 +1,9 @@ import { Inject, Injectable } from "@nestjs/common"; -import { Account } from "../contracts/models/account"; -import { Registration } from "../contracts/models/registration"; import * as mailVerificationService from "../contracts/services/mail.verification.service"; import { AccountService } from "./account.service"; import { AccountFactoryService } from "./account.factory.service"; +import { IRegistration } from "../contracts/models/registration"; +import { IAccount } from "../contracts/models/account"; @Injectable() export class RegistrationService { @@ -13,10 +13,10 @@ export class RegistrationService { @Inject(AccountFactoryService) private readonly accountFactory: AccountFactoryService, @Inject("@apihub24/mail_verification_service") - private readonly mailVerificationService: mailVerificationService.MailVerificationService + private readonly mailVerificationService: mailVerificationService.IMailVerificationService ) {} - async registerAccount(registration: Registration): Promise { + async registerAccount(registration: IRegistration): Promise { const newAccount = await this.accountFactory.createFromRegistration( registration ); diff --git a/src/services/right.factory.service.ts b/src/services/right.factory.service.ts index e4026d6..08d9e29 100644 --- a/src/services/right.factory.service.ts +++ b/src/services/right.factory.service.ts @@ -1,10 +1,11 @@ import { Injectable } from "@nestjs/common"; -import { Right } from "../contracts/models/right"; import { validate } from "class-validator"; +import { IRight } from "../contracts/models/right"; +import { Right } from "../models/right"; @Injectable() export class RightFactoryService { - async createRight(name: string): Promise { + async createRight(name: string): Promise { const right = new Right(); right.name = name; const validationErrors = await validate(right); diff --git a/src/services/right.service.ts b/src/services/right.service.ts index 0876283..2fd9335 100644 --- a/src/services/right.service.ts +++ b/src/services/right.service.ts @@ -1,22 +1,22 @@ -import { Right } from '../contracts/models/right'; -import { Inject, Injectable } from '@nestjs/common'; -import * as repository from '@apihub24/repository'; +import { Inject, Injectable } from "@nestjs/common"; +import * as repository from "@apihub24/repository"; +import { IRight } from "../contracts/models/right"; @Injectable() export class RightService { constructor( - @Inject('@apihub24/right_repository') - private readonly rightRepository: repository.Repository, + @Inject("@apihub24/right_repository") + private readonly rightRepository: repository.IRepository ) {} - async getBy(filter: (right: Right) => boolean): Promise { + async getBy(filter: (right: IRight) => boolean): Promise { return await this.rightRepository.getBy(filter); } - async save(right: Right): Promise { + async save(right: IRight): Promise { const rights = await this.rightRepository.save([right]); return rights?.length ? rights[0] : null; } - async delete(filter: (right: Right) => boolean): Promise { + async delete(filter: (right: IRight) => boolean): Promise { return await this.rightRepository.deleteBy(filter); } }