Skip to content

Commit

Permalink
fix: database duplicate key error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicToast committed Aug 27, 2024
1 parent d92498d commit a692846
Show file tree
Hide file tree
Showing 38 changed files with 2,112 additions and 1,752 deletions.
38 changes: 16 additions & 22 deletions libs/auth-domain/src/lib/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ export class AuthService {

async createUser(data: AuthData): Promise<Result<AuthAnemic>> {
try {
const checkEmail = await this.repository.findByEmail(data.email);
const checkUsername = await this.repository.findByUsername(
data.username,
);
if (checkEmail !== null) {
const checkEmail = await this.findByEmail(data.email);
if (checkEmail.isSuccess) {
return Result.fail<AuthAnemic>(AuthErrorCodes.EMAIL_FOUND);
}
if (checkUsername !== null) {
const checkUsername = await this.findByUsername(data.username);
if (checkUsername.isSuccess) {
return Result.fail<AuthAnemic>(AuthErrorCodes.USERNAME_FOUND);
}
const aggregate = this.factory.createDomain(data);
Expand All @@ -73,26 +71,22 @@ export class AuthService {
password: string,
): Promise<Result<AuthAnemic>> {
try {
const checkUsername =
await this.repository.findByUsername(username);
if (checkUsername === null) {
const checkUsername = await this.findByUsername(username);
if (checkUsername.isFailure) {
return Result.fail<AuthAnemic>(UserErrorCodes.NOT_FOUND);
}
if (checkUsername.password !== password) {
if (checkUsername.value.password !== password) {
return Result.fail<AuthAnemic>(AuthErrorCodes.INVALID_PASSWORD);
}
if (checkUsername.isBanned) {
if (checkUsername.value.isBanned) {
return Result.fail<AuthAnemic>(UserErrorCodes.IS_BANNED);
}
if (!checkUsername.isActive) {
if (!checkUsername.value.isActive) {
return Result.fail<AuthAnemic>(UserErrorCodes.NOT_ACTIVE);
}
//
const aggregate = this.factory.reconstitute(checkUsername);
const aggregate = this.factory.reconstitute(checkUsername.value);
aggregate.updateLogin();
await this.save(aggregate.toAnemic());
//
return Result.ok(checkUsername);
return await this.save(aggregate.toAnemic());
} catch (error) {
return Result.fail<AuthAnemic>(error);
}
Expand All @@ -103,17 +97,17 @@ export class AuthService {
token: string,
): Promise<Result<AuthAnemic>> {
try {
const result = await this.repository.findByEmail(email);
if (result !== null) {
if (result.isActive) {
const result = await this.findByEmail(email);
if (result.isSuccess) {
if (result.value.isActive) {
return Result.fail<AuthAnemic>(AuthErrorCodes.IS_ACTIVE);
}
if (result.activation_token !== token) {
if (result.value.activation_token !== token) {
return Result.fail<AuthAnemic>(
AuthErrorCodes.TOKEN_UNMATCH,
);
}
const aggregate = this.factory.reconstitute(result);
const aggregate = this.factory.reconstitute(result.value);
aggregate.updateActivation();
return await this.save(aggregate.toAnemic());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { CategoryAnemic } from '../anemics';
import { Chainable, Nullable } from '@toxictoast/azkaban-base-types';

interface CategoryAdditions {
findByParentId(parentId: Nullable<string>): Promise<Array<CategoryAnemic>>;
findByParentId(parentId: Nullable<string>): Promise<Array<CategoryAnemic>>;
findByTitle(title: string): Promise<CategoryAnemic>;
}

export type CategoryRepository = Chainable<
CategoryAdditions,
Repository<CategoryAnemic>
CategoryAdditions,
Repository<CategoryAnemic>
>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Repository } from '@toxictoast/azkaban-base-domain';
import { CompanyAnemic } from '../anemics';
import { Chainable } from '@toxictoast/azkaban-base-types';

export type CompanyRepository = Repository<CompanyAnemic>;
interface CompanyAdditions {
findByTitle(title: string): Promise<CompanyAnemic>;
}

export type CompanyRepository = Chainable<
CompanyAdditions,
Repository<CompanyAnemic>
>;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ItemVariantAdditions {
findByWarehouseId(
warehouseId: Nullable<string>,
): Promise<Array<ItemVariantAnemic>>;
findByTitle(title: string): Promise<ItemVariantAnemic>;
}

export type ItemVariantRepository = Chainable<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Repository } from '@toxictoast/azkaban-base-domain';
import { ItemAnemic } from '../anemics';
import { Chainable } from '@toxictoast/azkaban-base-types';

export type ItemRepository = Repository<ItemAnemic>;
interface ItemAdditions {
findByTitle(title: string): Promise<ItemAnemic>;
}

export type ItemRepository = Chainable<ItemAdditions, Repository<ItemAnemic>>;
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { LocationAnemic } from '../anemics';
import { Chainable, Nullable } from '@toxictoast/azkaban-base-types';

interface LocationAdditions {
findByParentId(parentId: Nullable<string>): Promise<Array<LocationAnemic>>;
findByFreezer(freezer: boolean): Promise<Array<LocationAnemic>>;
findByParentId(parentId: Nullable<string>): Promise<Array<LocationAnemic>>;
findByFreezer(freezer: boolean): Promise<Array<LocationAnemic>>;
findByTitle(title: string): Promise<LocationAnemic>;
}

export type LocationRepository = Chainable<
LocationAdditions,
Repository<LocationAnemic>
LocationAdditions,
Repository<LocationAnemic>
>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Repository } from '@toxictoast/azkaban-base-domain';
import { SizeAnemic } from '../anemics';
import { Chainable } from '@toxictoast/azkaban-base-types';

export type SizeRepository = Repository<SizeAnemic>;
interface SizeAdditions {
findByTitle(title: string): Promise<SizeAnemic>;
}

export type SizeRepository = Chainable<SizeAdditions, Repository<SizeAnemic>>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Repository } from '@toxictoast/azkaban-base-domain';
import { TypeAnemic } from '../anemics';
import { Chainable } from '@toxictoast/azkaban-base-types';

export type TypeRepository = Repository<TypeAnemic>;
interface TypeAdditions {
findByTitle(title: string): Promise<TypeAnemic>;
}

export type TypeRepository = Chainable<TypeAdditions, Repository<TypeAnemic>>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Repository } from '@toxictoast/azkaban-base-domain';
import { WarehouseAnemic } from '../anemics';
import { Chainable } from '@toxictoast/azkaban-base-types';

export type WarehouseRepository = Repository<WarehouseAnemic>;
interface WarehouseAdditions {
findByTitle(title: string): Promise<WarehouseAnemic>;
}

export type WarehouseRepository = Chainable<
WarehouseAdditions,
Repository<WarehouseAnemic>
>;
Loading

0 comments on commit a692846

Please sign in to comment.