chore: Move customer + customer group integration tests and fixes issues (#7577)

* chore: Move customer + customer group and fixes issues

* remove /customer sendpoint
This commit is contained in:
Oli Juhl
2024-06-03 10:52:32 +02:00
committed by GitHub
parent 9608bf06ef
commit 65d3222973
14 changed files with 804 additions and 1269 deletions
@@ -1,12 +1,13 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import {
deleteCustomerGroupsWorkflow,
updateCustomerGroupsWorkflow,
} from "@medusajs/core-flows"
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { MedusaError } from "@medusajs/utils"
import { refetchCustomerGroup } from "../helpers"
import { AdminUpdateCustomerGroupType } from "../validators"
@@ -20,6 +21,13 @@ export const GET = async (
req.remoteQueryConfig.fields
)
if (!customerGroup) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Customer group with id: ${req.params.id} not found`
)
}
res.status(200).json({ customer_group: customerGroup })
}
@@ -27,8 +35,7 @@ export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateCustomerGroupType>,
res: MedusaResponse
) => {
const updateGroups = updateCustomerGroupsWorkflow(req.scope)
await updateGroups.run({
await updateCustomerGroupsWorkflow(req.scope).run({
input: {
selector: { id: req.params.id },
update: req.validatedBody,
@@ -1,14 +1,14 @@
import * as QueryConfig from "./query-config"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { validateAndTransformBody } from "../../utils/validate-body"
import { validateAndTransformQuery } from "../../utils/validate-query"
import { createLinkBody } from "../../utils/validators"
import * as QueryConfig from "./query-config"
import {
AdminCreateCustomerGroup,
AdminGetCustomerGroupParams,
AdminGetCustomerGroupsParams,
AdminUpdateCustomerGroup,
} from "./validators"
import { validateAndTransformBody } from "../../utils/validate-body"
import { createLinkBody } from "../../utils/validators"
export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
{
@@ -5,6 +5,7 @@ export const defaultAdminCustomerGroupFields = [
"created_at",
"updated_at",
"deleted_at",
"metadata",
]
export const retrieveTransformQueryConfig = {
@@ -1,9 +1,9 @@
import { z } from "zod"
import {
createFindParams,
createOperatorMap,
createSelectParams,
} from "../../utils/validators"
import { z } from "zod"
export type AdminGetCustomerGroupParamsType = z.infer<
typeof AdminGetCustomerGroupParams
@@ -58,6 +58,7 @@ export type AdminCreateCustomerGroupType = z.infer<
>
export const AdminCreateCustomerGroup = z.object({
name: z.string(),
metadata: z.record(z.any()).optional(),
})
export type AdminUpdateCustomerGroupType = z.infer<
@@ -65,4 +66,5 @@ export type AdminUpdateCustomerGroupType = z.infer<
>
export const AdminUpdateCustomerGroup = z.object({
name: z.string(),
metadata: z.record(z.any()).optional(),
})
@@ -2,6 +2,7 @@ import {
deleteCustomersWorkflow,
updateCustomersWorkflow,
} from "@medusajs/core-flows"
import { AdminCustomer } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import {
AuthenticatedMedusaRequest,
@@ -9,7 +10,6 @@ import {
} from "../../../../types/routing"
import { refetchCustomer } from "../helpers"
import { AdminUpdateCustomerType } from "../validators"
import { AdminCustomer } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -13,8 +13,26 @@ export const defaultAdminCustomerFields = [
"deleted_at",
]
export const allowed = [
"id",
"company_name",
"first_name",
"last_name",
"email",
"phone",
"metadata",
"has_account",
"created_by",
"created_at",
"updated_at",
"deleted_at",
"addresses",
"groups",
]
export const retrieveTransformQueryConfig = {
defaults: defaultAdminCustomerFields,
allowed,
isList: false,
}
@@ -1,5 +1,6 @@
import { createCustomersWorkflow } from "@medusajs/core-flows"
import { AdminCustomer, PaginatedResponse } from "@medusajs/types"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
@@ -8,9 +9,8 @@ import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { AdminCreateCustomerType } from "./validators"
import { refetchCustomer } from "./helpers"
import { AdminCustomer, PaginatedResponse } from "@medusajs/types"
import { AdminCreateCustomerType } from "./validators"
export const GET = async (
req: AuthenticatedMedusaRequest,
@@ -388,7 +388,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"nullable": false,
"mappedType": "text"
},
"metadata": {
@@ -445,6 +445,16 @@
"name": "customer_group",
"schema": "public",
"indexes": [
{
"keyName": "IDX_customer_group_name_unique",
"columnNames": [
"name"
],
"composite": false,
"primary": false,
"unique": false,
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_customer_group_name_unique\" ON \"customer_group\" (name) WHERE deleted_at IS NULL"
},
{
"keyName": "customer_group_pkey",
"columnNames": [
@@ -0,0 +1,19 @@
import { Migration } from "@mikro-orm/migrations"
export class Migration20240602110946 extends Migration {
async up(): Promise<void> {
this.addSql(
'ALTER TABLE IF EXISTS "customer_group" ALTER COLUMN "name" SET NOT NULL;'
)
this.addSql(
'CREATE UNIQUE INDEX IF NOT EXISTS "IDX_customer_group_name_unique" ON "customer_group" (name) WHERE deleted_at IS NULL;'
)
}
async down(): Promise<void> {
this.addSql(
'ALTER TABLE IF EXISTS "customer_group" ALTER COLUMN "name" DROP NOT NULL;'
)
this.addSql('drop index if exists "IDX_customer_group_name_unique";')
}
}
@@ -1,21 +1,33 @@
import { DAL } from "@medusajs/types"
import { DALUtils, Searchable, generateEntityId } from "@medusajs/utils"
import {
DALUtils,
Searchable,
createPsqlIndexStatementHelper,
generateEntityId,
} from "@medusajs/utils"
import {
BeforeCreate,
Collection,
Entity,
Filter,
ManyToMany,
OnInit,
OptionalProps,
PrimaryKey,
Property,
ManyToMany,
Collection,
Filter,
} from "@mikro-orm/core"
import Customer from "./customer"
import CustomerGroupCustomer from "./customer-group-customer"
type OptionalGroupProps = DAL.SoftDeletableEntityDateColumns // TODO: To be revisited when more clear
const CustomerGroupUniqueName = createPsqlIndexStatementHelper({
tableName: "customer_group",
columns: ["name"],
unique: true,
where: "deleted_at IS NULL",
})
@Entity({ tableName: "customer_group" })
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
export default class CustomerGroup {
@@ -25,8 +37,9 @@ export default class CustomerGroup {
id!: string
@Searchable()
@Property({ columnType: "text", nullable: true })
name: string | null = null
@CustomerGroupUniqueName.MikroORMIndex()
@Property({ columnType: "text" })
name: string
@ManyToMany({
entity: () => Customer,