Sead amin
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Customer } from '../customers/entities/customer.entity';
|
||||
import { User } from '../users/entities/user.entity';
|
||||
import { SeedService } from './seed.service';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User, Customer])],
|
||||
providers: [SeedService],
|
||||
})
|
||||
export class DatabaseModule {}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
const ADMIN_MOBILES = ['09185290775', '09188630559'];
|
||||
|
||||
export class FixAdminUsersSeed1720800000000 implements MigrationInterface {
|
||||
name = 'FixAdminUsersSeed1720800000000';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
for (const mobile of ADMIN_MOBILES) {
|
||||
await queryRunner.query(
|
||||
`
|
||||
DELETE FROM customers
|
||||
WHERE "userId" IN (
|
||||
SELECT id FROM users WHERE mobile = $1
|
||||
)
|
||||
`,
|
||||
[mobile],
|
||||
);
|
||||
|
||||
const existingUsers: Array<{ id: string }> = await queryRunner.query(
|
||||
`SELECT id FROM users WHERE mobile = $1`,
|
||||
[mobile],
|
||||
);
|
||||
|
||||
if (existingUsers.length > 0) {
|
||||
await queryRunner.query(
|
||||
`UPDATE users SET role = 'admin' WHERE mobile = $1`,
|
||||
[mobile],
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const firstName = mobile === '09185290775' ? 'hamid' : 'ادمین';
|
||||
const lastName = mobile === '09185290775' ? 'Zarghami' : 'گرو';
|
||||
|
||||
await queryRunner.query(
|
||||
`
|
||||
INSERT INTO users ("firstName", "lastName", mobile, role)
|
||||
VALUES ($1, $2, $3, 'admin')
|
||||
`,
|
||||
[firstName, lastName, mobile],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
for (const mobile of ADMIN_MOBILES) {
|
||||
await queryRunner.query(`DELETE FROM users WHERE mobile = $1`, [mobile]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Customer } from '../customers/entities/customer.entity';
|
||||
import { User } from '../users/entities/user.entity';
|
||||
import { UserRole } from '../users/enums/user-role.enum';
|
||||
import { ADMIN_USERS_SEED } from './seeds/admin-users.seed';
|
||||
|
||||
@Injectable()
|
||||
export class SeedService implements OnModuleInit {
|
||||
private readonly logger = new Logger(SeedService.name);
|
||||
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private readonly usersRepository: Repository<User>,
|
||||
@InjectRepository(Customer)
|
||||
private readonly customersRepository: Repository<Customer>,
|
||||
) {}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
await this.seedAdminUsers();
|
||||
}
|
||||
|
||||
private async seedAdminUsers(): Promise<void> {
|
||||
for (const adminSeed of ADMIN_USERS_SEED) {
|
||||
const existingUser = await this.usersRepository.findOneBy({
|
||||
mobile: adminSeed.mobile,
|
||||
});
|
||||
|
||||
if (!existingUser) {
|
||||
await this.usersRepository.save({
|
||||
firstName: adminSeed.firstName,
|
||||
lastName: adminSeed.lastName,
|
||||
mobile: adminSeed.mobile,
|
||||
role: UserRole.ADMIN,
|
||||
});
|
||||
this.logger.log(`Created admin user for ${adminSeed.mobile}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (existingUser.role === UserRole.CUSTOMER) {
|
||||
await this.customersRepository.delete({ userId: existingUser.id });
|
||||
existingUser.role = UserRole.ADMIN;
|
||||
await this.usersRepository.save(existingUser);
|
||||
this.logger.log(
|
||||
`Converted customer ${adminSeed.mobile} to admin and removed customer profile`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (existingUser.role !== UserRole.ADMIN) {
|
||||
this.logger.warn(
|
||||
`Admin seed mobile ${adminSeed.mobile} is registered as ${existingUser.role}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
export interface AdminUserSeed {
|
||||
mobile: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
}
|
||||
|
||||
export const ADMIN_USERS_SEED: AdminUserSeed[] = [
|
||||
{ mobile: '09185290775', firstName: 'hamid', lastName: 'Zarghami' },
|
||||
{ mobile: '09188630559', firstName: 'abolfazl', lastName: 'faraji' },
|
||||
];
|
||||
|
||||
export const ADMIN_MOBILES = ADMIN_USERS_SEED.map((admin) => admin.mobile);
|
||||
Reference in New Issue
Block a user