-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into docs/sidebar-subscribers
- Loading branch information
Showing
48 changed files
with
1,170 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/dist | ||
node_modules | ||
.DS_store | ||
.env* | ||
.env | ||
*.sql |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Customer Module | ||
|
||
Customers represent entities who can make purchases in a store. | ||
|
||
### Features | ||
|
||
- The customer module enables you to store your customers’s contact details. | ||
- The customer module enables you to group your customers. |
41 changes: 41 additions & 0 deletions
41
packages/customer/integration-tests/__tests__/services/customer-module/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ICustomerModuleService } from "@medusajs/types" | ||
import { initialize } from "../../../../src/initialize" | ||
import { DB_URL, MikroOrmWrapper } from "../../../utils" | ||
|
||
jest.setTimeout(30000) | ||
|
||
describe("Customer Module Service", () => { | ||
let service: ICustomerModuleService | ||
|
||
beforeEach(async () => { | ||
await MikroOrmWrapper.setupDatabase() | ||
|
||
service = await initialize({ | ||
database: { | ||
clientUrl: DB_URL, | ||
schema: process.env.MEDUSA_CUSTOMER_DB_SCHEMA, | ||
}, | ||
}) | ||
}) | ||
|
||
afterEach(async () => { | ||
await MikroOrmWrapper.clearDatabase() | ||
}) | ||
|
||
describe("create", () => { | ||
it("should create a customer", async () => { | ||
const customerPromise = service.create({ | ||
first_name: "John", | ||
last_name: "Doe", | ||
}) | ||
|
||
await expect(customerPromise).resolves.toEqual( | ||
expect.objectContaining({ | ||
id: expect.any(String), | ||
first_name: "John", | ||
last_name: "Doe", | ||
}) | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
if (typeof process.env.DB_TEMP_NAME === "undefined") { | ||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1") | ||
process.env.DB_TEMP_NAME = `medusa-customer-integration-${tempName}` | ||
} | ||
|
||
process.env.MEDUSA_CUSTOMER_DB_SCHEMA = "public" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { JestUtils } from "medusa-test-utils" | ||
|
||
JestUtils.afterAllHookDropDatabase() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ModuleServiceInitializeOptions } from "@medusajs/types" | ||
|
||
export const databaseOptions: ModuleServiceInitializeOptions["database"] = { | ||
schema: "public", | ||
clientUrl: "medusa-customer-test", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TestDatabaseUtils } from "medusa-test-utils" | ||
|
||
import * as Models from "@models" | ||
|
||
const pathToMigrations = "../../src/migrations" | ||
const mikroOrmEntities = Models as unknown as any[] | ||
|
||
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper( | ||
mikroOrmEntities, | ||
pathToMigrations | ||
) | ||
|
||
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig( | ||
mikroOrmEntities, | ||
pathToMigrations | ||
) | ||
|
||
export const DB_URL = TestDatabaseUtils.getDatabaseURL() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./config" | ||
export * from "./database" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = { | ||
moduleNameMapper: { | ||
"^@models": "<rootDir>/src/models", | ||
"^@services": "<rootDir>/src/services", | ||
"^@repositories": "<rootDir>/src/repositories", | ||
}, | ||
transform: { | ||
"^.+\\.[jt]s?$": [ | ||
"ts-jest", | ||
{ | ||
tsConfig: "tsconfig.spec.json", | ||
isolatedModules: true, | ||
}, | ||
], | ||
}, | ||
testEnvironment: `node`, | ||
moduleFileExtensions: [`js`, `ts`], | ||
modulePathIgnorePatterns: ["dist/"], | ||
setupFiles: ["<rootDir>/integration-tests/setup-env.js"], | ||
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as entities from "./src/models" | ||
|
||
module.exports = { | ||
entities: Object.values(entities), | ||
schema: "public", | ||
clientUrl: "postgres://postgres@localhost/medusa-customer", | ||
type: "postgresql", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"name": "@medusajs/customer", | ||
"version": "0.0.1", | ||
"description": "Medusa Customer module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"bin": { | ||
"medusa-customer-seed": "dist/scripts/bin/run-seed.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/medusajs/medusa", | ||
"directory": "packages/customer" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"author": "Medusa", | ||
"license": "MIT", | ||
"scripts": { | ||
"watch": "tsc --build --watch", | ||
"watch:test": "tsc --build tsconfig.spec.json --watch", | ||
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json", | ||
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json", | ||
"test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts", | ||
"test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts", | ||
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate", | ||
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial", | ||
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create", | ||
"migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up", | ||
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear" | ||
}, | ||
"devDependencies": { | ||
"@medusajs/types": "workspace:^", | ||
"@mikro-orm/cli": "5.9.7", | ||
"cross-env": "^5.2.1", | ||
"jest": "^29.6.3", | ||
"medusa-test-utils": "^1.1.40", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"tsc-alias": "^1.8.6", | ||
"typescript": "^5.1.6" | ||
}, | ||
"dependencies": { | ||
"@medusajs/modules-sdk": "^1.12.5", | ||
"@medusajs/utils": "^1.11.2", | ||
"@mikro-orm/core": "5.9.7", | ||
"@mikro-orm/migrations": "5.9.7", | ||
"@mikro-orm/postgresql": "5.9.7", | ||
"awilix": "^8.0.0", | ||
"dotenv": "^16.1.4", | ||
"knex": "2.4.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { ModulesSdkUtils } from "@medusajs/utils" | ||
import * as Models from "@models" | ||
|
||
import { moduleDefinition } from "./module-definition" | ||
|
||
export default moduleDefinition | ||
|
||
const migrationScriptOptions = { | ||
moduleName: Modules.CUSTOMER, | ||
models: Models, | ||
pathToMigrations: __dirname + "/migrations", | ||
} | ||
|
||
export const revertMigration = ModulesSdkUtils.buildRevertMigrationScript( | ||
migrationScriptOptions | ||
) | ||
|
||
export const runMigration = ModulesSdkUtils.buildMigrationScript( | ||
migrationScriptOptions | ||
) | ||
|
||
export * from "./initialize" | ||
export * from "./loaders" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
ExternalModuleDeclaration, | ||
InternalModuleDeclaration, | ||
MedusaModule, | ||
MODULE_PACKAGE_NAMES, | ||
Modules, | ||
} from "@medusajs/modules-sdk" | ||
import { ICustomerModuleService, ModulesSdkTypes } from "@medusajs/types" | ||
import { moduleDefinition } from "../module-definition" | ||
import { InitializeModuleInjectableDependencies } from "../types" | ||
|
||
export const initialize = async ( | ||
options?: | ||
| ModulesSdkTypes.ModuleServiceInitializeOptions | ||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions | ||
| ExternalModuleDeclaration | ||
| InternalModuleDeclaration, | ||
injectedDependencies?: InitializeModuleInjectableDependencies | ||
): Promise<ICustomerModuleService> => { | ||
const loaded = await MedusaModule.bootstrap<ICustomerModuleService>({ | ||
moduleKey: Modules.CUSTOMER, | ||
defaultPath: MODULE_PACKAGE_NAMES[Modules.CUSTOMER], | ||
declaration: options as | ||
| InternalModuleDeclaration | ||
| ExternalModuleDeclaration, | ||
injectedDependencies, | ||
moduleExports: moduleDefinition, | ||
}) | ||
|
||
return loaded[Modules.CUSTOMER] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { ModuleJoinerConfig } from "@medusajs/types" | ||
import { MapToConfig } from "@medusajs/utils" | ||
import { Customer } from "@models" | ||
|
||
export const LinkableKeys = { | ||
customer_id: Customer.name, | ||
} | ||
|
||
const entityLinkableKeysMap: MapToConfig = {} | ||
Object.entries(LinkableKeys).forEach(([key, value]) => { | ||
entityLinkableKeysMap[value] ??= [] | ||
entityLinkableKeysMap[value].push({ | ||
mapTo: key, | ||
valueFrom: key.split("_").pop()!, | ||
}) | ||
}) | ||
|
||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap | ||
|
||
export const joinerConfig: ModuleJoinerConfig = { | ||
serviceName: Modules.CUSTOMER, | ||
primaryKeys: ["id"], | ||
linkableKeys: LinkableKeys, | ||
alias: { | ||
name: ["customer", "customers"], | ||
args: { | ||
entity: Customer.name, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
InternalModuleDeclaration, | ||
LoaderOptions, | ||
Modules, | ||
} from "@medusajs/modules-sdk" | ||
import { ModulesSdkTypes } from "@medusajs/types" | ||
import { ModulesSdkUtils } from "@medusajs/utils" | ||
import { EntitySchema } from "@mikro-orm/core" | ||
import * as CustomerModels from "../models" | ||
|
||
export default async ( | ||
{ | ||
options, | ||
container, | ||
logger, | ||
}: LoaderOptions< | ||
| ModulesSdkTypes.ModuleServiceInitializeOptions | ||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions | ||
>, | ||
moduleDeclaration?: InternalModuleDeclaration | ||
): Promise<void> => { | ||
const entities = Object.values(CustomerModels) as unknown as EntitySchema[] | ||
const pathToMigrations = __dirname + "/../migrations" | ||
|
||
await ModulesSdkUtils.mikroOrmConnectionLoader({ | ||
moduleName: Modules.CUSTOMER, | ||
entities, | ||
container, | ||
options, | ||
moduleDeclaration, | ||
logger, | ||
pathToMigrations, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as defaultRepositories from "@repositories" | ||
|
||
import { LoaderOptions } from "@medusajs/modules-sdk" | ||
import { ModulesSdkTypes } from "@medusajs/types" | ||
import { loadCustomRepositories } from "@medusajs/utils" | ||
import * as defaultServices from "@services" | ||
import { asClass } from "awilix" | ||
|
||
export default async ({ | ||
container, | ||
options, | ||
}: LoaderOptions< | ||
| ModulesSdkTypes.ModuleServiceInitializeOptions | ||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions | ||
>): Promise<void> => { | ||
const customRepositories = ( | ||
options as ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions | ||
)?.repositories | ||
|
||
container.register({ | ||
customerService: asClass(defaultServices.CustomerService).singleton(), | ||
addressService: asClass(defaultServices.AddressService).singleton(), | ||
customerGroupService: asClass( | ||
defaultServices.CustomerGroupService | ||
).singleton(), | ||
}) | ||
|
||
if (customRepositories) { | ||
loadCustomRepositories({ | ||
defaultRepositories, | ||
customRepositories, | ||
container, | ||
}) | ||
} else { | ||
loadDefaultRepositories({ container }) | ||
} | ||
} | ||
|
||
function loadDefaultRepositories({ container }) { | ||
container.register({ | ||
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(), | ||
customerRepository: asClass( | ||
defaultRepositories.CustomerRepository | ||
).singleton(), | ||
addressRepository: asClass( | ||
defaultRepositories.AddressRepository | ||
).singleton(), | ||
customerGroupRepository: asClass( | ||
defaultRepositories.CustomerGroupRepository | ||
).singleton(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./connection" | ||
export * from "./container" |
Oops, something went wrong.