chore: Move factories and helpers to a better place (#4551)
* chore: Move factories and helpers to a better place * align factory product variant * fix factory cart * add simple store fac * fix tests * fix tests * fix * fix cart seeder
This commit is contained in:
1 parent
26e2f81c24
commit
4d326fbbdf
197 files changed
+1073
-4332
No files matched your search
@@ -0,0 +1,60 @@
|
||||
import faker from "faker"
|
||||
import { DataSource } from "typeorm"
|
||||
import { Customer, CustomerGroup } from "@medusajs/medusa"
|
||||
import { CustomerGroupFactoryData, simpleCustomerGroupFactory, } from "./simple-customer-group-factory"
|
||||
|
||||
export type CustomerFactoryData = {
|
||||
id?: string
|
||||
email?: string
|
||||
phone?: string
|
||||
first_name?: string
|
||||
last_name?: string
|
||||
groups?: CustomerGroupFactoryData[]
|
||||
password_hash?: string
|
||||
has_account?: boolean
|
||||
}
|
||||
|
||||
export const simpleCustomerFactory = async (
|
||||
dataSource: DataSource,
|
||||
data: CustomerFactoryData = {},
|
||||
seed?: number
|
||||
): Promise<Customer> => {
|
||||
if (typeof seed !== "undefined") {
|
||||
faker.seed(seed)
|
||||
}
|
||||
|
||||
const manager = dataSource.manager
|
||||
|
||||
const customerId = data.id || `simple-customer-${Math.random() * 1000}`
|
||||
const c = manager.create(Customer, {
|
||||
id: customerId,
|
||||
email: data.email ?? faker.internet.email(),
|
||||
phone: data.phone ?? faker.phone.phoneNumber(),
|
||||
first_name: data.first_name ?? faker.name.firstName(),
|
||||
last_name: data.last_name ?? faker.name.lastName(),
|
||||
password_hash:
|
||||
data.password_hash ??
|
||||
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
|
||||
has_account: data.has_account ?? true,
|
||||
})
|
||||
|
||||
if (data.password_hash) {
|
||||
c.password_hash = data.password_hash
|
||||
c.has_account = true
|
||||
}
|
||||
|
||||
const customer = await manager.save(c)
|
||||
|
||||
if (data.groups) {
|
||||
const groups: CustomerGroup[] = []
|
||||
for (const g of data.groups) {
|
||||
const created = await simpleCustomerGroupFactory(dataSource, g)
|
||||
groups.push(created)
|
||||
}
|
||||
|
||||
customer.groups = groups
|
||||
await manager.save(customer)
|
||||
}
|
||||
|
||||
return customer
|
||||
}
|
||||
Reference in new issue
Block a user