Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { UserService } from 'src/user/user.service';
import { EmailModule } from 'src/email/email.module';
import { AuthGuard } from './auth.guard';
import { BranchModule } from 'src/branch/branch.module';

@Module({
imports: [
forwardRef(() => UserModule),
forwardRef(() => BranchModule),
ConfigModule,
JwtModule.registerAsync({
useFactory: (configService: ConfigService) => ({
Expand Down
5 changes: 4 additions & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export class AuthService {

async updatePassword(user: User, newPasswod: string): Promise<boolean> {
const password = await this.encryptPassword(newPasswod);
await this.userService.update(user, { password });
await this.userService.update(user, {
password,
isGenericPassword: false,
});
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions src/branch/branch.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { BranchService } from './branch.service';
import { BranchController } from './branch.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
Expand All @@ -14,9 +14,10 @@ import { State } from 'src/state/entities/state.entity';
@Module({
imports: [
TypeOrmModule.forFeature([Branch, City, State, Country]),
AuthModule,
forwardRef(() => AuthModule),
],
controllers: [BranchController],
providers: [BranchService, CityService, StateService, CountryService],
exports: [BranchService],
})
export class BranchModule {}
19 changes: 19 additions & 0 deletions src/user/dto/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
MaxLength,
MinLength,
IsString,
IsUUID,
IsBoolean,
} from 'class-validator';
import { UserGender } from '../entities/profile.entity';
import { IsOlderThan } from 'src/utils/is-older-than-validator';
Expand Down Expand Up @@ -88,6 +90,14 @@ export class BaseUserDTO {
@IsOptional()
@IsEnum(UserGender)
gender?: UserGender;

@IsOptional()
@IsBoolean()
@Expose()
@ApiProperty({
description: 'Indicates whether the user has a generic password',
})
isGenericPassword?: boolean;
}

export class UserDTO extends IntersectionType(BaseUserDTO, PasswordDTO) {}
Expand All @@ -98,6 +108,15 @@ export class UserAdminDTO extends BaseUserDTO {
@Expose()
role: UserRole;

@ApiProperty({
description: 'branchId of the user (branch_admin or delivery)',
required: false,
})
@IsOptional()
@IsUUID()
@Expose()
branchId?: string;

// Data use if it is a delivery

@ApiProperty({ description: 'Motorcycle brand', required: false })
Expand Down
6 changes: 6 additions & 0 deletions src/user/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ export class User extends BaseModel {

@Column({ type: 'character varying', name: 'ws_id', nullable: true })
wsId: string | undefined;

@Column({ type: 'boolean', default: false, name: 'is_mobile_customer' })
isMobileCustomer: boolean;

@Column({ type: 'boolean', default: false, name: 'is_generic_password' })
isGenericPassword: boolean;
}
25 changes: 25 additions & 0 deletions src/user/migrations/1747259587010-add-flags-to-user-migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddFlagsToUserMigration1747259587010
implements MigrationInterface
{
name = 'AddFlagsToUserMigration1747259587010';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "user" ADD "is_mobile_customer" boolean NOT NULL DEFAULT false`,
);
await queryRunner.query(
`ALTER TABLE "user" ADD "is_generic_password" boolean NOT NULL DEFAULT false`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "user" DROP COLUMN "is_generic_password"`,
);
await queryRunner.query(
`ALTER TABLE "user" DROP COLUMN "is_mobile_customer"`,
);
}
}
4 changes: 4 additions & 0 deletions src/user/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { AuthModule } from 'src/auth/auth.module';
import { Branch } from 'src/branch/entities/branch.entity';
import { UserAddress } from './entities/user-address.entity';
import { UserMoto } from './entities/user-moto.entity';
import { BranchModule } from 'src/branch/branch.module';
import { EmailModule } from 'src/email/email.module';

@Module({
imports: [
Expand All @@ -21,6 +23,8 @@ import { UserMoto } from './entities/user-moto.entity';
UserMoto,
]),
forwardRef(() => AuthModule),
forwardRef(() => BranchModule),
EmailModule,
],
providers: [UserService],
exports: [UserService, TypeOrmModule],
Expand Down
26 changes: 26 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { CreateUserAddressDTO } from './dto/user-address.dto';
import { ConfigService } from '@nestjs/config';
import { UserMoto } from './entities/user-moto.entity';
import { UpdateUserMotoDTO } from './dto/user-moto.dto';
import { BranchService } from 'src/branch/branch.service';
import { EmailService } from 'src/email/email.service';

@Injectable()
export class UserService {
Expand All @@ -31,6 +33,9 @@ export class UserService {
@InjectRepository(UserMoto)
private UserMotoRepository: Repository<UserMoto>,
private configService: ConfigService,

private readonly branchService: BranchService,
private emailService: EmailService,
) {}

async userExists(options: Partial<User>): Promise<boolean> {
Expand Down Expand Up @@ -126,7 +131,28 @@ export class UserService {
const hashedPassword = await bcrypt.hash(password, 10);
const newUser = this.userRepository.create(user);
newUser.password = hashedPassword;
newUser.isGenericPassword = true;
if (
user.role === UserRole.BRANCH_ADMIN ||
user.role === UserRole.DELIVERY
) {
if (!user.branchId) {
throw new BadRequestException('branchId is required for this role');
}

const branch = await this.branchService.findOne(user.branchId);

newUser.branch = branch;
}
const userCreated = await this.userRepository.save(newUser);

await this.emailService.sendEmail({
recipients: [{ email: user.email, name: user.firstName }],
subject: 'Welcome to Pharmatech',
html: `<p>Hi, your account has been created. Your password is: <b>${password}</b></p>`,
text: `Hi, your account has been created. Your password is: ${password}`,
});

const profile = new Profile();
profile.user = userCreated;
if (user.gender) {
Expand Down
Loading