update seeder
This commit is contained in:
@@ -59,7 +59,7 @@ export class AdminController {
|
|||||||
@ApiOperation({ summary: 'Delete an admin by ID' })
|
@ApiOperation({ summary: 'Delete an admin by ID' })
|
||||||
@ApiParam({ name: 'id', description: 'Admin ID' })
|
@ApiParam({ name: 'id', description: 'Admin ID' })
|
||||||
deleteById(@Param('adminId') adminId: string,): Promise<void> {
|
deleteById(@Param('adminId') adminId: string,): Promise<void> {
|
||||||
return this.adminService.remove(adminId,);
|
return this.adminService.softDelete(adminId,);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,13 +23,22 @@ export class AdminService {
|
|||||||
|
|
||||||
const normalizedPhone = normalizePhone(phone);
|
const normalizedPhone = normalizePhone(phone);
|
||||||
|
|
||||||
const exist = await this.adminRepository.findOne({
|
const currentAdmin = await this.adminRepository.findOne({
|
||||||
phone: normalizedPhone,
|
phone: normalizedPhone,
|
||||||
});
|
});
|
||||||
if (exist) {
|
|
||||||
|
if (currentAdmin && !currentAdmin.deletedAt) {
|
||||||
throw new BadRequestException('This phone number is already used');
|
throw new BadRequestException('This phone number is already used');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO : test admins that were delted
|
||||||
|
// re-register deleted admin
|
||||||
|
if (currentAdmin && currentAdmin.deletedAt) {
|
||||||
|
currentAdmin.deletedAt = undefined
|
||||||
|
await this.em.flush()
|
||||||
|
return currentAdmin
|
||||||
|
}
|
||||||
|
|
||||||
const role = await this.roleRepository.findOne({ id: roleId })
|
const role = await this.roleRepository.findOne({ id: roleId })
|
||||||
if (!role) {
|
if (!role) {
|
||||||
throw new BadRequestException('Role not found');
|
throw new BadRequestException('Role not found');
|
||||||
@@ -127,14 +136,13 @@ export class AdminService {
|
|||||||
return admin;
|
return admin;
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(adminId: string): Promise<void> {
|
async softDelete(adminId: string): Promise<void> {
|
||||||
const admin = await this.adminRepository.findOne({ id: adminId });
|
const admin = await this.adminRepository.findOne({ id: adminId });
|
||||||
if (!admin) {
|
if (!admin) {
|
||||||
throw new NotFoundException('Admin role not found');
|
throw new NotFoundException('Admin role not found');
|
||||||
}
|
}
|
||||||
// admin.deletedAt=new Date()
|
admin.deletedAt = new Date()
|
||||||
// TODO :L is this correct to soft delete
|
return this.em.flush();
|
||||||
return this.em.removeAndFlush(admin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOrFail(adminId: string) {
|
async findOrFail(adminId: string) {
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import { ProductsSeeder } from './product.seeder';
|
|||||||
import { AdminsSeeder } from './admins.seeder';
|
import { AdminsSeeder } from './admins.seeder';
|
||||||
import { UsersSeeder } from './users.seeder';
|
import { UsersSeeder } from './users.seeder';
|
||||||
import { CategoriesSeeder } from './categories.seeder';
|
import { CategoriesSeeder } from './categories.seeder';
|
||||||
|
import { ProductsFieldsSeeder } from './product-fields.seeder';
|
||||||
|
import { FieldOption } from 'src/modules/form-builder/entities/field-option.entity';
|
||||||
|
import { FieldOptionSeeder } from './field-options.seeder';
|
||||||
// import { AttributesSeeder } from './attribute.seeder';
|
// import { AttributesSeeder } from './attribute.seeder';
|
||||||
// import { AttributeValueSeeder } from './attribute-value.seeder';
|
// import { AttributeValueSeeder } from './attribute-value.seeder';
|
||||||
|
|
||||||
@@ -42,17 +45,17 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
await productsSeeder.run(em, categoriesMap);
|
await productsSeeder.run(em, categoriesMap);
|
||||||
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Done`);
|
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Done`);
|
||||||
|
|
||||||
// 10. Create products
|
// 10. Create product Fields
|
||||||
// console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Creating attributes`);
|
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Creating product fields`);
|
||||||
// const attributesSeeder = new AttributesSeeder();
|
const productFieldsSeeder = new ProductsFieldsSeeder();
|
||||||
// await attributesSeeder.run(em);
|
await productFieldsSeeder.run(em);
|
||||||
// console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Done`);
|
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Done`);
|
||||||
|
|
||||||
// 11. Create products
|
// 11. Create product Field Options
|
||||||
// console.info(`[Seeder] Step 10/${TOTAL_STEPS}: Creating attributes`);
|
console.info(`[Seeder] Step 10/${TOTAL_STEPS}: Creating fields options`);
|
||||||
// const attributesValueSeeder = new AttributeValueSeeder();
|
const fieldOptionsSeeder = new FieldOptionSeeder();
|
||||||
// await attributesValueSeeder.run(em);
|
await fieldOptionsSeeder.run(em);
|
||||||
// console.info(`[Seeder] Step 11/${TOTAL_STEPS}: Done`);
|
console.info(`[Seeder] Step 11/${TOTAL_STEPS}: Done`);
|
||||||
|
|
||||||
// 12. Create Admins
|
// 12. Create Admins
|
||||||
console.info(`[Seeder] Step 12/${TOTAL_STEPS}: Creating admins`);
|
console.info(`[Seeder] Step 12/${TOTAL_STEPS}: Creating admins`);
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
// import type { EntityManager } from '@mikro-orm/core';
|
|
||||||
// import { attributeValueData } from './data/attribute-value.data';
|
|
||||||
|
|
||||||
// export class AttributeValueSeeder {
|
|
||||||
// async run(
|
|
||||||
// em: EntityManager,
|
|
||||||
|
|
||||||
// ): Promise<void> {
|
|
||||||
// const attributes = await em.findAll(Attribute)
|
|
||||||
// for (const attr of attributes) {
|
|
||||||
|
|
||||||
// const attrValues = attributeValueData.filter(a => a.attributeName === attr.name)
|
|
||||||
|
|
||||||
// for (const av of attrValues) {
|
|
||||||
|
|
||||||
// const a= em.create(AttributeValue, {
|
|
||||||
// value: av.value,
|
|
||||||
// attribute: attr
|
|
||||||
// });
|
|
||||||
|
|
||||||
// em.persist(a)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await em.flush();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
// import type { EntityManager } from '@mikro-orm/core';
|
|
||||||
// import { Product } from '../modules/product/entities/product.entity';
|
|
||||||
// import { attributeData } from './data/attribute.data';
|
|
||||||
// // import { Attribute } from 'src/modules/product/entities/attribute.entity';
|
|
||||||
|
|
||||||
// export class AttributesSeeder {
|
|
||||||
// async run(
|
|
||||||
// em: EntityManager,
|
|
||||||
|
|
||||||
// ): Promise<void> {
|
|
||||||
// const products = await em.findAll(Product)
|
|
||||||
// for (const product of products) {
|
|
||||||
// for (const attrData of attributeData) {
|
|
||||||
// const attr = em.create(Attribute, {
|
|
||||||
// name: attrData.name,
|
|
||||||
// type: attrData.type,
|
|
||||||
// isRequired: attrData.isRequired,
|
|
||||||
// order: 1,
|
|
||||||
// product
|
|
||||||
// });
|
|
||||||
|
|
||||||
// em.persist(attr);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// await em.flush();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@@ -8,7 +8,7 @@ export interface AttributeData {
|
|||||||
type: FieldType,
|
type: FieldType,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const attributeData: AttributeData[] = [
|
export const fieldsDataData: AttributeData[] = [
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -15,4 +15,25 @@ export const rolesData: RoleConfig[] = [
|
|||||||
permissionFilter: (name: PermissionEnum) => true
|
permissionFilter: (name: PermissionEnum) => true
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: 'admin',
|
||||||
|
title: 'مدیر داخلی',
|
||||||
|
isSystem: true,
|
||||||
|
permissionFilter: (name: PermissionEnum) => name !==PermissionEnum.MANAGE_ROLES
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: 'accountant',
|
||||||
|
title: 'حسابدار',
|
||||||
|
isSystem: true,
|
||||||
|
permissionFilter: (name: PermissionEnum) => name==PermissionEnum.MANAGE_PAYMENTS
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: 'designer',
|
||||||
|
title: 'طراح',
|
||||||
|
isSystem: true,
|
||||||
|
permissionFilter: (name: PermissionEnum) => [PermissionEnum.VIEW_MY_ASSIGNED_INVOICED_REQUESTS].includes(name)
|
||||||
|
},
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -1,63 +0,0 @@
|
|||||||
export interface UserAddressData {
|
|
||||||
userPhone: string;
|
|
||||||
title: string;
|
|
||||||
address: string;
|
|
||||||
city: string;
|
|
||||||
province?: string;
|
|
||||||
postalCode?: string;
|
|
||||||
latitude: number;
|
|
||||||
longitude: number;
|
|
||||||
phone?: string;
|
|
||||||
isDefault: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const userAddressesData: UserAddressData[] = [
|
|
||||||
{
|
|
||||||
userPhone: '09362532122',
|
|
||||||
title: 'خانه',
|
|
||||||
address: 'خیابان ولیعصر، پلاک 123',
|
|
||||||
city: 'تهران',
|
|
||||||
province: 'تهران',
|
|
||||||
postalCode: '1234567890',
|
|
||||||
latitude: 35.6892,
|
|
||||||
longitude: 51.389,
|
|
||||||
phone: '09362532122',
|
|
||||||
isDefault: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
userPhone: '09185290775',
|
|
||||||
title: 'خانه',
|
|
||||||
address: 'خیابان انقلاب، پلاک 456',
|
|
||||||
city: 'تهران',
|
|
||||||
province: 'تهران',
|
|
||||||
postalCode: '1234567891',
|
|
||||||
latitude: 35.69,
|
|
||||||
longitude: 51.39,
|
|
||||||
phone: '09185290775',
|
|
||||||
isDefault: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
userPhone: '09129283395',
|
|
||||||
title: 'خانه',
|
|
||||||
address: 'خیابان آزادی، پلاک 789',
|
|
||||||
city: 'تهران',
|
|
||||||
province: 'تهران',
|
|
||||||
postalCode: '1234567892',
|
|
||||||
latitude: 35.691,
|
|
||||||
longitude: 51.391,
|
|
||||||
phone: '09129283395',
|
|
||||||
isDefault: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
userPhone: '09184317567',
|
|
||||||
title: 'خانه',
|
|
||||||
address: 'خیابان جمهوری، پلاک 321',
|
|
||||||
city: 'تهران',
|
|
||||||
province: 'تهران',
|
|
||||||
postalCode: '1234567893',
|
|
||||||
latitude: 35.692,
|
|
||||||
longitude: 51.392,
|
|
||||||
phone: '09184317567',
|
|
||||||
isDefault: true,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import type { EntityManager } from '@mikro-orm/core';
|
||||||
|
import { attributeValueData } from './data/product-field-options.data';
|
||||||
|
import { Field } from 'src/modules/form-builder/entities/field.entity';
|
||||||
|
import { FieldOption } from 'src/modules/form-builder/entities/field-option.entity';
|
||||||
|
|
||||||
|
export class FieldOptionSeeder {
|
||||||
|
async run(
|
||||||
|
em: EntityManager,
|
||||||
|
|
||||||
|
): Promise<void> {
|
||||||
|
const fields = await em.findAll(Field)
|
||||||
|
|
||||||
|
for (const field of fields) {
|
||||||
|
|
||||||
|
const fieldOptions = attributeValueData.filter(a => a.attributeName === field.name)
|
||||||
|
|
||||||
|
for (const option of fieldOptions) {
|
||||||
|
|
||||||
|
const a= em.create(FieldOption, {
|
||||||
|
value: option.value,
|
||||||
|
field
|
||||||
|
});
|
||||||
|
|
||||||
|
em.persist(a)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
await em.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import type { EntityManager } from '@mikro-orm/core';
|
||||||
|
import { Product } from '../modules/product/entities/product.entity';
|
||||||
|
import { fieldsDataData } from './data/product-field.data';
|
||||||
|
import { Field } from 'src/modules/form-builder/entities/field.entity';
|
||||||
|
import { EntityType } from 'src/modules/form-builder/interface/print';
|
||||||
|
// import { Attribute } from 'src/modules/product/entities/attribute.entity';
|
||||||
|
|
||||||
|
export class ProductsFieldsSeeder {
|
||||||
|
async run(
|
||||||
|
em: EntityManager,
|
||||||
|
|
||||||
|
): Promise<void> {
|
||||||
|
const products = await em.findAll(Product)
|
||||||
|
for (const product of products) {
|
||||||
|
for (const fieldData of fieldsDataData) {
|
||||||
|
|
||||||
|
const field = em.create(Field, {
|
||||||
|
name: fieldData.name,
|
||||||
|
type: fieldData.type,
|
||||||
|
isRequired: fieldData.isRequired,
|
||||||
|
order: 1,
|
||||||
|
entityType: EntityType.product,
|
||||||
|
entityId: product.id,
|
||||||
|
multiple: false
|
||||||
|
});
|
||||||
|
|
||||||
|
em.persist(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await em.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user