feat: Update authentication middleware (#6447)

* authentication middleware update

* disable customer authentication

* call correct feature flag method

* fix authentication middleware for store/customers

* fix integration tests and add middleware for admin customers

* update seeders

* customer groups fix

* add authentication middleware for all admin endpoints

* Feat(medusa, user): require authentication for invite accept (#6448)

* initial invite token validation for authentication invocation

* remove invite auth

* remove unused import

* cleanup tests

* refactor to auth instead of auth_user

* pr feedback

* update authenticatedRequest type

* update store authenticated endpoints

* update routes with type

* fix build

* fix build

* fix build

* use auth middleware for api-keys
This commit is contained in:
Philip Korsholm authored and GitHub committed 2024-02-27 13:50:18 +08:00
1 parent 63aea44e06
commit 7bddb58542
94 files changed
+1177 -509

No files matched your search

@@ -1,12 +1,14 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ApiKeyType } from "@medusajs/utils"
import { IApiKeyModuleService } from "@medusajs/types" import { IApiKeyModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
import { ApiKeyType } from "@medusajs/utils"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -36,7 +38,7 @@ describe("API Keys - Admin", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -60,7 +62,7 @@ describe("API Keys - Admin", () => {
expect.objectContaining({ expect.objectContaining({
id: created.data.apiKey.id, id: created.data.apiKey.id,
title: "Test Secret Key", title: "Test Secret Key",
created_by: "test", created_by: "admin_user",
}) })
) )
// On create we get the token in raw form so we can store it. // On create we get the token in raw form so we can store it.
@@ -92,7 +94,7 @@ describe("API Keys - Admin", () => {
expect(revoked.data.apiKey).toEqual( expect(revoked.data.apiKey).toEqual(
expect.objectContaining({ expect.objectContaining({
id: created.data.apiKey.id, id: created.data.apiKey.id,
revoked_by: "test", revoked_by: "admin_user",
}) })
) )
expect(revoked.data.apiKey.revoked_at).toBeTruthy() expect(revoked.data.apiKey.revoked_at).toBeTruthy()
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("POST /admin/customer-groups/:id/customers/batch", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("DELETE /admin/customer-groups/:id/customers/remove", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,8 @@ describe("POST /admin/customer-groups", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
// await adminSeeder(dbConnection)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("DELETE /admin/customer-groups/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("GET /admin/customer-groups/:id/customers", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("GET /admin/customer-groups", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("GET /admin/customer-groups/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("POST /admin/customer-groups/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("POST /admin/customers/:id/addresses", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("POST /admin/customers", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("DELETE /admin/customers/:id/addresses/:address_id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("DELETE /admin/customers/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("GET /admin/customers/:id/addresses", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("GET /admin/customers", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("POST /admin/customers/:id/addresses/:address_id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ICustomerModuleService } from "@medusajs/types" import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +39,7 @@ describe("POST /admin/customers/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -3,6 +3,7 @@ import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../../../helpers/admin-seeder" import adminSeeder from "../../../../helpers/admin-seeder"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container" import { getContainer } from "../../../../environment-helpers/use-container"
import jwt from "jsonwebtoken" import jwt from "jsonwebtoken"
import path from "path" import path from "path"
@@ -0,0 +1,112 @@
import { IAuthModuleService, IUserModuleService } from "@medusajs/types"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
jest.setTimeout(50000)
const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}
describe("GET /admin/invites/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let userModuleService: IUserModuleService
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
appContainer = getContainer()
userModuleService = appContainer.resolve(ModuleRegistrationName.USER)
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders)
})
afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should fail to accept an invite with an invalid invite token", async () => {
const api = useApi()! as AxiosInstance
const authResponse = await api.post(`/auth/admin/emailpass`, {
email: "potential_member@test.com",
password: "supersecret",
})
expect(authResponse.status).toEqual(200)
const token = authResponse.data.token
const acceptResponse = await api
.post(
`/admin/invites/accept?token=${"non-existing-token"}`,
{
first_name: "John",
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.catch((e) => e)
expect(acceptResponse.response.status).toEqual(401)
expect(acceptResponse.response.data.message).toEqual("Unauthorized")
})
it("should accept an invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
})
const api = useApi()! as AxiosInstance
const authResponse = await api.post(`/auth/admin/emailpass`, {
email: "potential_member@test.com",
password: "supersecret",
})
expect(authResponse.status).toEqual(200)
const token = authResponse.data.token
const acceptResponse = await api.post(
`/admin/invites/accept?token=${invite.token}`,
{
first_name: "John",
},
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
expect(acceptResponse.status).toEqual(200)
expect(acceptResponse.data.user).toEqual(
expect.objectContaining({
email: "potential_member@test.com",
first_name: "John",
})
)
})
})
@@ -1,11 +1,12 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +38,7 @@ describe("POST /admin/campaigns", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,12 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { PromotionType } from "@medusajs/utils" import { PromotionType } from "@medusajs/utils"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -38,7 +39,7 @@ describe("POST /admin/promotions", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,12 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +38,7 @@ describe("DELETE /admin/campaigns/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,12 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +38,7 @@ describe("DELETE /admin/promotions/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,12 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types"
import { CampaignBudgetType } from "@medusajs/utils" import { CampaignBudgetType } from "@medusajs/utils"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -69,7 +70,7 @@ describe("GET /admin/campaigns", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
await promotionModuleService.createCampaigns(campaignsData) await promotionModuleService.createCampaigns(campaignsData)
}) })
@@ -1,12 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { PromotionType } from "@medusajs/utils" import { PromotionType } from "@medusajs/utils"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -38,7 +39,7 @@ describe("GET /admin/promotions", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,12 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types"
import { CampaignBudgetType } from "@medusajs/utils" import { CampaignBudgetType } from "@medusajs/utils"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -52,7 +53,7 @@ describe("GET /admin/campaigns", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,12 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { PromotionType } from "@medusajs/utils" import { PromotionType } from "@medusajs/utils"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -38,7 +39,7 @@ describe("GET /admin/promotions", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,12 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -37,7 +38,7 @@ describe("POST /admin/campaigns/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,12 +1,13 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IPromotionModuleService } from "@medusajs/types" import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { PromotionType } from "@medusajs/utils" import { PromotionType } from "@medusajs/utils"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -38,7 +39,7 @@ describe("POST /admin/promotions/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterEach(async () => { afterEach(async () => {
@@ -1,11 +1,12 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { IRegionModuleService } from "@medusajs/types" import { IRegionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../../helpers/create-admin-user"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api" import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -35,7 +36,7 @@ describe("Regions - Admin", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
await service.createDefaultCountriesAndCurrencies() await service.createDefaultCountriesAndCurrencies()
}) })
@@ -1,13 +1,10 @@
import { initDb, useDb } from "../../../environment-helpers/use-db" import { initDb, useDb } from "../../../environment-helpers/use-db"
import { IUserModuleService } from "@medusajs/types" import { AxiosInstance } from "axios"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api" import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -27,7 +24,7 @@ describe("POST /admin/users", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterAll(async () => { afterAll(async () => {
@@ -1,13 +1,13 @@
import { initDb, useDb } from "../../../environment-helpers/use-db" import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { IUserModuleService } from "@medusajs/types" import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container" import { getContainer } from "../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api" import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -31,7 +31,7 @@ describe("DELETE /admin/users/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterAll(async () => { afterAll(async () => {
@@ -1,13 +1,13 @@
import { initDb, useDb } from "../../../environment-helpers/use-db" import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { IUserModuleService } from "@medusajs/types" import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container" import { getContainer } from "../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api" import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -31,7 +31,7 @@ describe("GET /admin/users", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterAll(async () => { afterAll(async () => {
@@ -1,13 +1,13 @@
import { initDb, useDb } from "../../../environment-helpers/use-db" import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { IUserModuleService } from "@medusajs/types" import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container" import { getContainer } from "../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api" import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -31,7 +31,7 @@ describe("GET /admin/users/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterAll(async () => { afterAll(async () => {
@@ -1,13 +1,13 @@
import { initDb, useDb } from "../../../environment-helpers/use-db" import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios"
import { IUserModuleService } from "@medusajs/types" import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container" import { getContainer } from "../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api" import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"
jest.setTimeout(50000) jest.setTimeout(50000)
@@ -31,7 +31,7 @@ describe("POST /admin/users/:id", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterAll(async () => { afterAll(async () => {
@@ -1,17 +1,18 @@
import { useApi } from "../../../environment-helpers/use-api"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { import {
createStep, createStep,
createWorkflow, createWorkflow,
StepResponse, StepResponse,
WorkflowData, WorkflowData,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { initDb, useDb } from "../../../environment-helpers/use-db"
import { AxiosInstance } from "axios" import { AxiosInstance } from "axios"
import adminSeeder from "../../../helpers/admin-seeder"
import { createAdminUser } from "../../helpers/create-admin-user"
import { getContainer } from "../../../environment-helpers/use-container"
import path from "path" import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app" import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { getContainer } from "../../../environment-helpers/use-container" import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
export const workflowEngineTestSuite = (env, extraParams = {}) => { export const workflowEngineTestSuite = (env, extraParams = {}) => {
const adminHeaders = { const adminHeaders = {
@@ -31,7 +32,7 @@ export const workflowEngineTestSuite = (env, extraParams = {}) => {
shutdownServer = await startBootstrapApp({ cwd, env }) shutdownServer = await startBootstrapApp({ cwd, env })
medusaContainer = getContainer() medusaContainer = getContainer()
await adminSeeder(dbConnection) await createAdminUser(dbConnection, adminHeaders)
}) })
afterAll(async () => { afterAll(async () => {
@@ -1,8 +1,8 @@
import { IAuthModuleService } from "@medusajs/types" import { IAuthModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import adminSeeder from "../../helpers/admin-seeder" import adminSeeder from "../../helpers/admin-seeder"
import jwt from "jsonwebtoken"
import { getContainer } from "../../environment-helpers/use-container" import { getContainer } from "../../environment-helpers/use-container"
import jwt from "jsonwebtoken"
export const createAdminUser = async (dbConnection, adminHeaders) => { export const createAdminUser = async (dbConnection, adminHeaders) => {
await adminSeeder(dbConnection) await adminSeeder(dbConnection)
@@ -43,6 +43,17 @@ module.exports = {
scope: "internal", scope: "internal",
resources: "shared", resources: "shared",
resolve: "@medusajs/auth", resolve: "@medusajs/auth",
options: {
providers: [
{
name: "emailpass",
scopes: {
admin: {},
store: {},
},
},
],
},
}, },
[Modules.USER]: { [Modules.USER]: {
scope: "internal", scope: "internal",
@@ -1,15 +1,22 @@
import { revokeApiKeysWorkflow } from "@medusajs/core-flows" import {
import { RevokeApiKeyDTO } from "@medusajs/types" AuthenticatedMedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" MedusaResponse,
} from "../../../../../types/routing"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { import { RevokeApiKeyDTO } from "@medusajs/types"
import { revokeApiKeysWorkflow } from "@medusajs/core-flows"
export const POST = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const { result, errors } = await revokeApiKeysWorkflow(req.scope).run({ const { result, errors } = await revokeApiKeysWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
revoke: { revoke: {
revoked_by: req.auth_user?.id, revoked_by: req.auth.actor_id,
} as RevokeApiKeyDTO, } as RevokeApiKeyDTO,
}, },
throwOnError: false, throwOnError: false,
@@ -1,13 +1,20 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { import {
deleteApiKeysWorkflow, deleteApiKeysWorkflow,
updateApiKeysWorkflow, updateApiKeysWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { UpdateApiKeyDTO } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { defaultAdminApiKeyFields } from "../query-config"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { UpdateApiKeyDTO } from "@medusajs/types"
import { defaultAdminApiKeyFields } from "../query-config"
import { remoteQueryObjectFromString } from "@medusajs/utils"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve("remoteQuery") const remoteQuery = req.scope.resolve("remoteQuery")
const variables = { id: req.params.id } const variables = { id: req.params.id }
@@ -23,11 +30,14 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ apiKey }) res.status(200).json({ apiKey })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<Omit<UpdateApiKeyDTO, "id">>,
res: MedusaResponse
) => {
const { result, errors } = await updateApiKeysWorkflow(req.scope).run({ const { result, errors } = await updateApiKeysWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody as Omit<UpdateApiKeyDTO, "id">, update: req.validatedBody,
}, },
throwOnError: false, throwOnError: false,
}) })
@@ -39,7 +49,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ apiKey: result[0] }) res.status(200).json({ apiKey: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const { errors } = await deleteApiKeysWorkflow(req.scope).run({ const { errors } = await deleteApiKeysWorkflow(req.scope).run({
@@ -1,25 +1,21 @@
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { import {
AdminGetApiKeysParams,
AdminGetApiKeysApiKeyParams, AdminGetApiKeysApiKeyParams,
AdminPostApiKeysReq, AdminGetApiKeysParams,
AdminPostApiKeysApiKeyReq, AdminPostApiKeysApiKeyReq,
AdminPostApiKeysReq,
AdminRevokeApiKeysApiKeyReq, AdminRevokeApiKeysApiKeyReq,
} from "./validators" } from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminApiKeyRoutesMiddlewares: MiddlewareRoute[] = [ export const adminApiKeyRoutesMiddlewares: MiddlewareRoute[] = [
{ {
matcher: "/admin/api-keys*", matcher: "/admin/api-keys*",
// middlewares: [authenticate("admin", ["bearer", "session"])], middlewares: [authenticate("admin", ["bearer", "session"])],
// TODO: Apply authentication middleware correctly once https://github.com/medusajs/medusa/pull/6447 is merged.
middlewares: [
(req, res, next) => {
req.auth_user = { id: "test" }
next()
},
],
}, },
{ {
method: ["GET"], method: ["GET"],
@@ -1,10 +1,17 @@
import { createApiKeysWorkflow } from "@medusajs/core-flows" import {
import { CreateApiKeyDTO } from "@medusajs/types" AuthenticatedMedusaRequest,
import { remoteQueryObjectFromString } from "@medusajs/utils" MedusaResponse,
import { MedusaRequest, MedusaResponse } from "../../../types/routing" } from "../../../types/routing"
import { defaultAdminApiKeyFields } from "./query-config"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { CreateApiKeyDTO } from "@medusajs/types"
import { createApiKeysWorkflow } from "@medusajs/core-flows"
import { defaultAdminApiKeyFields } from "./query-config"
import { remoteQueryObjectFromString } from "@medusajs/utils"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve("remoteQuery") const remoteQuery = req.scope.resolve("remoteQuery")
const queryObject = remoteQueryObjectFromString({ const queryObject = remoteQueryObjectFromString({
@@ -28,11 +35,14 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<Omit<CreateApiKeyDTO, "created_by">>,
res: MedusaResponse
) => {
const input = [ const input = [
{ {
...(req.validatedBody as Omit<CreateApiKeyDTO, "created_by">), ...req.validatedBody,
created_by: req.auth_user?.id, created_by: req.auth.actor_id,
} as CreateApiKeyDTO, } as CreateApiKeyDTO,
] ]
@@ -1,12 +1,22 @@
import {
AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { import {
deleteCampaignsWorkflow, deleteCampaignsWorkflow,
updateCampaignsWorkflow, updateCampaignsWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { AdminPostCampaignsReq } from "../validators"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { UpdateCampaignDTO } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const promotionModuleService: IPromotionModuleService = req.scope.resolve( const promotionModuleService: IPromotionModuleService = req.scope.resolve(
ModuleRegistrationName.PROMOTION ModuleRegistrationName.PROMOTION
) )
@@ -22,14 +32,17 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ campaign }) res.status(200).json({ campaign })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostCampaignsReq>,
res: MedusaResponse
) => {
const updateCampaigns = updateCampaignsWorkflow(req.scope) const updateCampaigns = updateCampaignsWorkflow(req.scope)
const campaignsData = [ const campaignsData = [
{ {
id: req.params.id, id: req.params.id,
...(req.validatedBody || {}), ...req.validatedBody,
}, },
] ] as UpdateCampaignDTO[]
const { result, errors } = await updateCampaigns.run({ const { result, errors } = await updateCampaigns.run({
input: { campaignsData }, input: { campaignsData },
@@ -43,7 +56,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ campaign: result[0] }) res.status(200).json({ campaign: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const manager = req.scope.resolve("manager") const manager = req.scope.resolve("manager")
const deleteCampaigns = deleteCampaignsWorkflow(req.scope) const deleteCampaigns = deleteCampaignsWorkflow(req.scope)
@@ -1,22 +1,25 @@
import { MedusaV2Flag } from "@medusajs/utils"
import {
isFeatureFlagEnabled,
transformBody,
transformQuery,
} from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { import {
AdminGetCampaignsCampaignParams, AdminGetCampaignsCampaignParams,
AdminGetCampaignsParams, AdminGetCampaignsParams,
AdminPostCampaignsCampaignReq, AdminPostCampaignsCampaignReq,
AdminPostCampaignsReq, AdminPostCampaignsReq,
} from "./validators" } from "./validators"
import {
isFeatureFlagEnabled,
transformBody,
transformQuery,
} from "../../../api/middlewares"
import { MedusaV2Flag } from "@medusajs/utils"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminCampaignRoutesMiddlewares: MiddlewareRoute[] = [ export const adminCampaignRoutesMiddlewares: MiddlewareRoute[] = [
{ {
matcher: "/admin/campaigns*", matcher: "/admin/campaigns*",
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)], middlewares: [authenticate("admin", ["bearer", "session"])],
}, },
{ {
method: ["GET"], method: ["GET"],
@@ -1,9 +1,17 @@
import { createCampaignsWorkflow } from "@medusajs/core-flows" import {
import { ModuleRegistrationName } from "@medusajs/modules-sdk" AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { CreateCampaignDTO, IPromotionModuleService } from "@medusajs/types" import { CreateCampaignDTO, IPromotionModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createCampaignsWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const promotionModuleService: IPromotionModuleService = req.scope.resolve( const promotionModuleService: IPromotionModuleService = req.scope.resolve(
ModuleRegistrationName.PROMOTION ModuleRegistrationName.PROMOTION
) )
@@ -23,9 +31,12 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateCampaignDTO>,
res: MedusaResponse
) => {
const createCampaigns = createCampaignsWorkflow(req.scope) const createCampaigns = createCampaignsWorkflow(req.scope)
const campaignsData = [req.validatedBody as CreateCampaignDTO] const campaignsData = [req.validatedBody]
const { result, errors } = await createCampaigns.run({ const { result, errors } = await createCampaigns.run({
input: { campaignsData }, input: { campaignsData },
@@ -1,5 +1,4 @@
import { CampaignBudgetType } from "@medusajs/utils" import { FindParams, extendedFindParamsMixin } from "../../../types/common"
import { Type } from "class-transformer"
import { import {
IsArray, IsArray,
IsDateString, IsDateString,
@@ -10,7 +9,10 @@ import {
IsString, IsString,
ValidateNested, ValidateNested,
} from "class-validator" } from "class-validator"
import { FindParams, extendedFindParamsMixin } from "../../../types/common" import { Transform, Type } from "class-transformer"
import { CampaignBudgetType } from "@medusajs/utils"
import { transformOptionalDate } from "../../../utils/validators/date-transform"
export class AdminGetCampaignsCampaignParams extends FindParams {} export class AdminGetCampaignsCampaignParams extends FindParams {}
@@ -1,11 +1,18 @@
import { createCustomerGroupCustomersWorkflow } from "@medusajs/core-flows" import {
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing" AuthenticatedMedusaRequest,
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators" MedusaResponse,
} from "../../../../../../types/routing"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"
import { createCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
export const POST = async (
// eslint-disable-next-line max-len
req: AuthenticatedMedusaRequest<AdminPostCustomerGroupsGroupCustomersBatchReq>,
res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const { customer_ids } = const { customer_ids } = req.validatedBody
req.validatedBody as AdminPostCustomerGroupsGroupCustomersBatchReq
const createCustomers = createCustomerGroupCustomersWorkflow(req.scope) const createCustomers = createCustomerGroupCustomersWorkflow(req.scope)
@@ -1,12 +1,18 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators"
import { deleteCustomerGroupCustomersWorkflow } from "@medusajs/core-flows" import { deleteCustomerGroupCustomersWorkflow } from "@medusajs/core-flows"
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing" export const POST = async (
import { AdminPostCustomerGroupsGroupCustomersBatchReq } from "../../../validators" // eslint-disable-next-line max-len
req: AuthenticatedMedusaRequest<AdminPostCustomerGroupsGroupCustomersBatchReq>,
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const { customer_ids } = const { customer_ids } = req.validatedBody
req.validatedBody as AdminPostCustomerGroupsGroupCustomersBatchReq
const deleteCustomers = deleteCustomerGroupCustomersWorkflow(req.scope) const deleteCustomers = deleteCustomerGroupCustomersWorkflow(req.scope)
@@ -1,8 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { ICustomerModuleService } from "@medusajs/types" AuthenticatedMedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" MedusaResponse,
} from "../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ICustomerModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const service = req.scope.resolve<ICustomerModuleService>( const service = req.scope.resolve<ICustomerModuleService>(
@@ -1,15 +1,22 @@
import { import {
updateCustomerGroupsWorkflow, AuthenticatedMedusaRequest,
deleteCustomerGroupsWorkflow, MedusaResponse,
} from "@medusajs/core-flows" } from "../../../../types/routing"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
CustomerGroupUpdatableFields, CustomerGroupUpdatableFields,
ICustomerModuleService, ICustomerModuleService,
} from "@medusajs/types" } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" import {
deleteCustomerGroupsWorkflow,
updateCustomerGroupsWorkflow,
} from "@medusajs/core-flows"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
) )
@@ -25,12 +32,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ customer_group: group }) res.status(200).json({ customer_group: group })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CustomerGroupUpdatableFields>,
res: MedusaResponse
) => {
const updateGroups = updateCustomerGroupsWorkflow(req.scope) const updateGroups = updateCustomerGroupsWorkflow(req.scope)
const { result, errors } = await updateGroups.run({ const { result, errors } = await updateGroups.run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody as CustomerGroupUpdatableFields, update: req.validatedBody,
}, },
throwOnError: false, throwOnError: false,
}) })
@@ -42,7 +52,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ customer_group: result[0] }) res.status(200).json({ customer_group: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const deleteCustomerGroups = deleteCustomerGroupsWorkflow(req.scope) const deleteCustomerGroups = deleteCustomerGroupsWorkflow(req.scope)
@@ -1,16 +1,19 @@
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { listTransformQueryConfig as customersListTransformQueryConfig } from "../customers/query-config"
import { import {
AdminGetCustomerGroupsParams,
AdminGetCustomerGroupsGroupParams,
AdminPostCustomerGroupsReq,
AdminPostCustomerGroupsGroupReq,
AdminGetCustomerGroupsGroupCustomersParams,
AdminPostCustomerGroupsGroupCustomersBatchReq,
AdminDeleteCustomerGroupsGroupCustomersBatchReq, AdminDeleteCustomerGroupsGroupCustomersBatchReq,
AdminGetCustomerGroupsGroupCustomersParams,
AdminGetCustomerGroupsGroupParams,
AdminGetCustomerGroupsParams,
AdminPostCustomerGroupsGroupCustomersBatchReq,
AdminPostCustomerGroupsGroupReq,
AdminPostCustomerGroupsReq,
} from "./validators" } from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
import { listTransformQueryConfig as customersListTransformQueryConfig } from "../customers/query-config"
export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [ export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
{ {
@@ -23,6 +26,11 @@ export const adminCustomerGroupRoutesMiddlewares: MiddlewareRoute[] = [
), ),
], ],
}, },
{
method: ["ALL"],
matcher: "/admin/customer-groups*",
middlewares: [authenticate("admin", ["bearer", "session"])],
},
{ {
method: ["GET"], method: ["GET"],
matcher: "/admin/customer-groups/:id", matcher: "/admin/customer-groups/:id",
@@ -1,9 +1,17 @@
import { createCustomerGroupsWorkflow } from "@medusajs/core-flows" import {
import { ModuleRegistrationName } from "@medusajs/modules-sdk" AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { CreateCustomerGroupDTO, ICustomerModuleService } from "@medusajs/types" import { CreateCustomerGroupDTO, ICustomerModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createCustomerGroupsWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
) )
@@ -24,12 +32,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateCustomerGroupDTO>,
res: MedusaResponse
) => {
const createGroups = createCustomerGroupsWorkflow(req.scope) const createGroups = createCustomerGroupsWorkflow(req.scope)
const customersData = [ const customersData = [
{ {
...(req.validatedBody as CreateCustomerGroupDTO), ...req.validatedBody,
created_by: req.user!.id, created_by: req.auth.actor_id,
}, },
] ]
@@ -1,12 +1,19 @@
import { import {
updateCustomerAddressesWorkflow, AuthenticatedMedusaRequest,
deleteCustomerAddressesWorkflow, MedusaResponse,
} from "@medusajs/core-flows" } from "../../../../../../types/routing"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { CustomerAddressDTO, ICustomerModuleService } from "@medusajs/types" import { CustomerAddressDTO, ICustomerModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing" import {
deleteCustomerAddressesWorkflow,
updateCustomerAddressesWorkflow,
} from "@medusajs/core-flows"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
) )
@@ -22,12 +29,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ address }) res.status(200).json({ address })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<Partial<CustomerAddressDTO>>,
res: MedusaResponse
) => {
const updateAddresses = updateCustomerAddressesWorkflow(req.scope) const updateAddresses = updateCustomerAddressesWorkflow(req.scope)
const { result, errors } = await updateAddresses.run({ const { result, errors } = await updateAddresses.run({
input: { input: {
selector: { id: req.params.address_id, customer_id: req.params.id }, selector: { id: req.params.address_id, customer_id: req.params.id },
update: req.validatedBody as Partial<CustomerAddressDTO>, update: req.validatedBody,
}, },
throwOnError: false, throwOnError: false,
}) })
@@ -39,7 +49,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ address: result[0] }) res.status(200).json({ address: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.address_id const id = req.params.address_id
const deleteAddress = deleteCustomerAddressesWorkflow(req.scope) const deleteAddress = deleteCustomerAddressesWorkflow(req.scope)
@@ -1,12 +1,19 @@
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows" import {
import { ModuleRegistrationName } from "@medusajs/modules-sdk" AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { import {
CreateCustomerAddressDTO, CreateCustomerAddressDTO,
ICustomerModuleService, ICustomerModuleService,
} from "@medusajs/types" } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerId = req.params.id const customerId = req.params.id
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
@@ -28,12 +35,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateCustomerAddressDTO>,
res: MedusaResponse
) => {
const customerId = req.params.id const customerId = req.params.id
const createAddresses = createCustomerAddressesWorkflow(req.scope) const createAddresses = createCustomerAddressesWorkflow(req.scope)
const addresses = [ const addresses = [
{ {
...(req.validatedBody as CreateCustomerAddressDTO), ...req.validatedBody,
customer_id: customerId, customer_id: customerId,
}, },
] ]
@@ -1,15 +1,22 @@
import { import {
updateCustomersWorkflow, AuthenticatedMedusaRequest,
deleteCustomersWorkflow, MedusaResponse,
} from "@medusajs/core-flows" } from "../../../../types/routing"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { import {
CustomerUpdatableFields, CustomerUpdatableFields,
ICustomerModuleService, ICustomerModuleService,
} from "@medusajs/types" } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" import {
deleteCustomersWorkflow,
updateCustomersWorkflow,
} from "@medusajs/core-flows"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
) )
@@ -22,12 +29,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ customer }) res.status(200).json({ customer })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CustomerUpdatableFields>,
res: MedusaResponse
) => {
const updateCustomers = updateCustomersWorkflow(req.scope) const updateCustomers = updateCustomersWorkflow(req.scope)
const { result, errors } = await updateCustomers.run({ const { result, errors } = await updateCustomers.run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody as CustomerUpdatableFields, update: req.validatedBody,
}, },
throwOnError: false, throwOnError: false,
}) })
@@ -39,7 +49,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ customer: result[0] }) res.status(200).json({ customer: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const deleteCustomers = deleteCustomersWorkflow(req.scope) const deleteCustomers = deleteCustomersWorkflow(req.scope)
@@ -1,17 +1,25 @@
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { import {
AdminGetCustomersParams,
AdminGetCustomersCustomerParams,
AdminPostCustomersReq,
AdminPostCustomersCustomerReq,
AdminPostCustomersCustomerAddressesReq,
AdminGetCustomersCustomerAddressesParams, AdminGetCustomersCustomerAddressesParams,
AdminGetCustomersCustomerParams,
AdminGetCustomersParams,
AdminPostCustomersCustomerAddressesAddressReq, AdminPostCustomersCustomerAddressesAddressReq,
AdminPostCustomersCustomerAddressesReq,
AdminPostCustomersCustomerReq,
AdminPostCustomersReq,
} from "./validators" } from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [ export const adminCustomerRoutesMiddlewares: MiddlewareRoute[] = [
{
method: ["ALL"],
matcher: "/admin/customers*",
middlewares: [authenticate("admin", ["bearer", "session"])],
},
{ {
method: ["GET"], method: ["GET"],
matcher: "/admin/customers", matcher: "/admin/customers",
@@ -1,9 +1,16 @@
import { createCustomersWorkflow } from "@medusajs/core-flows" import {
import { ModuleRegistrationName } from "@medusajs/modules-sdk" AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { CreateCustomerDTO, ICustomerModuleService } from "@medusajs/types" import { CreateCustomerDTO, ICustomerModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createCustomersWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
) )
@@ -41,12 +48,16 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateCustomerDTO>,
res: MedusaResponse
) => {
const createCustomers = createCustomersWorkflow(req.scope) const createCustomers = createCustomersWorkflow(req.scope)
const customersData = [ const customersData = [
{ {
...(req.validatedBody as CreateCustomerDTO), ...req.validatedBody,
created_by: req.user!.id, created_by: req.auth?.actor_id,
}, },
] ]
@@ -1,15 +1,20 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
MedusaError, MedusaError,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { deleteInvitesWorkflow } from "@medusajs/core-flows" import { deleteInvitesWorkflow } from "@medusajs/core-flows"
import { IUserModuleService, UpdateUserDTO } from "@medusajs/types"
import { ModuleRegistrationName } from "../../../../../../modules-sdk/dist"
// Get invite // Get invite
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
@@ -34,7 +39,10 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
} }
// delete invite // delete invite
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const workflow = deleteInvitesWorkflow(req.scope) const workflow = deleteInvitesWorkflow(req.scope)
@@ -1,16 +1,23 @@
import { acceptInviteWorkflow } from "@medusajs/core-flows" import {
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" AuthenticatedMedusaRequest,
import { InviteWorkflow } from "@medusajs/types" MedusaResponse,
} from "../../../../types/routing"
import { AdminPostInvitesInviteAcceptReq } from "../validators" import { AdminPostInvitesInviteAcceptReq } from "../validators"
import { IUserModuleService } from "@medusajs/types" import { IUserModuleService } from "@medusajs/types"
import { InviteWorkflow } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { acceptInviteWorkflow } from "@medusajs/core-flows"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
if (req.auth_user?.app_metadata?.user_id) { req: AuthenticatedMedusaRequest<AdminPostInvitesInviteAcceptReq>,
res: MedusaResponse
) => {
if (req.auth.actor_id) {
const moduleService: IUserModuleService = req.scope.resolve( const moduleService: IUserModuleService = req.scope.resolve(
ModuleRegistrationName.USER ModuleRegistrationName.USER
) )
const user = moduleService.retrieve(req.auth_user.app_metadata.user_id) const user = moduleService.retrieve(req.auth.actor_id)
res.status(200).json({ user }) res.status(200).json({ user })
return return
} }
@@ -19,11 +26,18 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const input = { const input = {
invite_token: req.filterableFields.token as string, invite_token: req.filterableFields.token as string,
auth_user_id: req.auth_user!.id, auth_user_id: req.auth?.auth_user_id,
user: req.validatedBody as AdminPostInvitesInviteAcceptReq, user: req.validatedBody,
} as InviteWorkflow.AcceptInviteWorkflowInputDTO } as InviteWorkflow.AcceptInviteWorkflowInputDTO
const { result: users } = await workflow.run({ input }) let users
try {
const { result } = await workflow.run({ input })
users = result
} catch (e) {
res.status(401).json({ message: "Unauthorized" })
return
}
// Set customer_id on session user if we are in session // Set customer_id on session user if we are in session
if (req.session.auth_user) { if (req.session.auth_user) {
@@ -1,19 +1,35 @@
import { transformBody, transformQuery } from "../../../api/middlewares" import * as QueryConfig from "./query-config"
import { import {
AdminCreateInviteRequest, AdminCreateInviteRequest,
AdminGetInvitesParams,
AdminGetInvitesInviteParams, AdminGetInvitesInviteParams,
AdminPostInvitesInviteAcceptReq, AdminGetInvitesParams,
AdminPostInvitesInviteAcceptParams, AdminPostInvitesInviteAcceptParams,
AdminPostInvitesInviteAcceptReq,
} from "./validators" } from "./validators"
import * as QueryConfig from "./query-config" import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../types/middlewares" import { MiddlewareRoute } from "../../../types/middlewares"
import { authenticate } from "../../../utils/authenticate-middleware" import { authenticate } from "../../../utils/authenticate-middleware"
export const adminInviteRoutesMiddlewares: MiddlewareRoute[] = [ export const adminInviteRoutesMiddlewares: MiddlewareRoute[] = [
{ {
method: "ALL", method: "ALL",
matcher: "/admin/invites*", matcher: "/admin/invites",
middlewares: [authenticate("admin", ["session", "bearer"])],
},
{
method: "POST",
matcher: "/admin/invites/accept",
middlewares: [
authenticate("admin", ["session", "bearer"], {
allowUnregistered: true,
}),
],
},
{
method: ["GET", "DELETE"],
matcher: "/admin/invites/:id",
middlewares: [authenticate("admin", ["session", "bearer"])], middlewares: [authenticate("admin", ["session", "bearer"])],
}, },
{ {
@@ -1,13 +1,20 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
import { CreateInviteDTO } from "@medusajs/types"
import { createInvitesWorkflow } from "@medusajs/core-flows" import { createInvitesWorkflow } from "@medusajs/core-flows"
import { CreateInviteDTO, CreateUserDTO } from "@medusajs/types"
// List invites // List invites
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const query = remoteQueryObjectFromString({ const query = remoteQueryObjectFromString({
@@ -34,12 +41,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
} }
// Create invite // Create invite
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateInviteDTO>,
res: MedusaResponse
) => {
const workflow = createInvitesWorkflow(req.scope) const workflow = createInvitesWorkflow(req.scope)
const input = { const input = {
input: { input: {
invites: [req.validatedBody as CreateInviteDTO], invites: [req.validatedBody],
}, },
} }
@@ -1,12 +1,22 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { import {
deletePromotionsWorkflow, deletePromotionsWorkflow,
updatePromotionsWorkflow, updatePromotionsWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { AdminPostPromotionsPromotionReq } from "../validators"
import { IPromotionModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { UpdateApplicationMethodDTO } from "@medusajs/types"
import { UpdatePromotionDTO } from "@medusajs/types"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const promotionModuleService: IPromotionModuleService = req.scope.resolve( const promotionModuleService: IPromotionModuleService = req.scope.resolve(
ModuleRegistrationName.PROMOTION ModuleRegistrationName.PROMOTION
) )
@@ -19,14 +29,17 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ promotion }) res.status(200).json({ promotion })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionReq>,
res: MedusaResponse
) => {
const updatePromotions = updatePromotionsWorkflow(req.scope) const updatePromotions = updatePromotionsWorkflow(req.scope)
const promotionsData = [ const promotionsData = [
{ {
id: req.params.id, id: req.params.id,
...(req.validatedBody || {}), ...req.validatedBody,
}, },
] ] as UpdatePromotionDTO[]
const { result, errors } = await updatePromotions.run({ const { result, errors } = await updatePromotions.run({
input: { promotionsData }, input: { promotionsData },
@@ -40,7 +53,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ promotion: result[0] }) res.status(200).json({ promotion: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const manager = req.scope.resolve("manager") const manager = req.scope.resolve("manager")
const deletePromotions = deletePromotionsWorkflow(req.scope) const deletePromotions = deletePromotionsWorkflow(req.scope)
@@ -1,23 +1,25 @@
import { MedusaV2Flag } from "@medusajs/utils"
import {
isFeatureFlagEnabled,
transformBody,
transformQuery,
} from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { import {
AdminGetPromotionsParams, AdminGetPromotionsParams,
AdminGetPromotionsPromotionParams, AdminGetPromotionsPromotionParams,
AdminPostPromotionsPromotionReq, AdminPostPromotionsPromotionReq,
AdminPostPromotionsReq, AdminPostPromotionsReq,
} from "./validators" } from "./validators"
import {
isFeatureFlagEnabled,
transformBody,
transformQuery,
} from "../../../api/middlewares"
import { MedusaV2Flag } from "@medusajs/utils"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
{ {
matcher: "/admin/promotions*", matcher: "/admin/promotions*",
middlewares: [isFeatureFlagEnabled(MedusaV2Flag.key)], middlewares: [authenticate("admin", ["bearer", "session"])],
}, },
{ {
method: ["GET"], method: ["GET"],
@@ -1,9 +1,16 @@
import { createPromotionsWorkflow } from "@medusajs/core-flows" import {
import { ModuleRegistrationName } from "@medusajs/modules-sdk" AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { CreatePromotionDTO, IPromotionModuleService } from "@medusajs/types" import { CreatePromotionDTO, IPromotionModuleService } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { createPromotionsWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const promotionModuleService: IPromotionModuleService = req.scope.resolve( const promotionModuleService: IPromotionModuleService = req.scope.resolve(
ModuleRegistrationName.PROMOTION ModuleRegistrationName.PROMOTION
) )
@@ -23,9 +30,12 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreatePromotionDTO>,
res: MedusaResponse
) => {
const createPromotions = createPromotionsWorkflow(req.scope) const createPromotions = createPromotionsWorkflow(req.scope)
const promotionsData = [req.validatedBody as CreatePromotionDTO] const promotionsData = [req.validatedBody]
const { result, errors } = await createPromotions.run({ const { result, errors } = await createPromotions.run({
input: { promotionsData }, input: { promotionsData },
@@ -1,13 +1,20 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { import {
deleteRegionsWorkflow, deleteRegionsWorkflow,
updateRegionsWorkflow, updateRegionsWorkflow,
} from "@medusajs/core-flows" } from "@medusajs/core-flows"
import { UpdateRegionDTO } from "@medusajs/types"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { defaultAdminRegionFields } from "../query-config"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { UpdateRegionDTO } from "@medusajs/types"
import { defaultAdminRegionFields } from "../query-config"
import { remoteQueryObjectFromString } from "@medusajs/utils"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve("remoteQuery") const remoteQuery = req.scope.resolve("remoteQuery")
const variables = { id: req.params.id } const variables = { id: req.params.id }
@@ -23,11 +30,14 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ region }) res.status(200).json({ region })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<UpdateRegionDTO>,
res: MedusaResponse
) => {
const { result, errors } = await updateRegionsWorkflow(req.scope).run({ const { result, errors } = await updateRegionsWorkflow(req.scope).run({
input: { input: {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody as UpdateRegionDTO, update: req.validatedBody,
}, },
throwOnError: false, throwOnError: false,
}) })
@@ -39,7 +49,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ region: result[0] }) res.status(200).json({ region: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.params.id const id = req.params.id
const { errors } = await deleteRegionsWorkflow(req.scope).run({ const { errors } = await deleteRegionsWorkflow(req.scope).run({
@@ -1,14 +1,22 @@
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { import {
AdminGetRegionsParams, AdminGetRegionsParams,
AdminGetRegionsRegionParams, AdminGetRegionsRegionParams,
AdminPostRegionsRegionReq, AdminPostRegionsRegionReq,
AdminPostRegionsReq, AdminPostRegionsReq,
} from "./validators" } from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [ export const adminRegionRoutesMiddlewares: MiddlewareRoute[] = [
{
method: ["ALL"],
matcher: "/admin/regions*",
middlewares: [authenticate("admin", ["bearer", "session"])],
},
{ {
method: ["GET"], method: ["GET"],
matcher: "/admin/regions", matcher: "/admin/regions",
@@ -1,10 +1,17 @@
import { createRegionsWorkflow } from "@medusajs/core-flows" import {
import { CreateRegionDTO } from "@medusajs/types" AuthenticatedMedusaRequest,
import { remoteQueryObjectFromString } from "@medusajs/utils" MedusaResponse,
import { MedusaRequest, MedusaResponse } from "../../../types/routing" } from "../../../types/routing"
import { defaultAdminRegionFields } from "./query-config"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { CreateRegionDTO } from "@medusajs/types"
import { createRegionsWorkflow } from "@medusajs/core-flows"
import { defaultAdminRegionFields } from "./query-config"
import { remoteQueryObjectFromString } from "@medusajs/utils"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve("remoteQuery") const remoteQuery = req.scope.resolve("remoteQuery")
const queryObject = remoteQueryObjectFromString({ const queryObject = remoteQueryObjectFromString({
@@ -28,10 +35,13 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateRegionDTO>,
res: MedusaResponse
) => {
const input = [ const input = [
{ {
...(req.validatedBody as CreateRegionDTO), ...req.validatedBody,
}, },
] ]
@@ -1,15 +1,18 @@
import { import {
ContainerRegistrationKeys, AuthenticatedMedusaRequest,
remoteQueryObjectFromString, MedusaResponse,
} from "@medusajs/utils" } from "../../../../types/routing"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { deleteUsersWorkflow, updateUsersWorkflow } from "@medusajs/core-flows"
import { IUserModuleService, UpdateUserDTO } from "@medusajs/types" import { IUserModuleService, UpdateUserDTO } from "@medusajs/types"
import { ModuleRegistrationName } from "../../../../../../modules-sdk/dist" import { deleteUsersWorkflow, updateUsersWorkflow } from "@medusajs/core-flows"
import { AdminUpdateUserRequest } from "../validators" import { AdminUpdateUserRequest } from "../validators"
import { ModuleRegistrationName } from "../../../../../../modules-sdk/dist"
// Get user // Get user
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const moduleService: IUserModuleService = req.scope.resolve( const moduleService: IUserModuleService = req.scope.resolve(
@@ -21,14 +24,17 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
} }
// update user // update user
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<AdminUpdateUserRequest>,
res: MedusaResponse
) => {
const workflow = updateUsersWorkflow(req.scope) const workflow = updateUsersWorkflow(req.scope)
const input = { const input = {
updates: [ updates: [
{ {
id: req.params.id, id: req.params.id,
...(req.validatedBody as AdminUpdateUserRequest), ...req.validatedBody,
} as UpdateUserDTO, } as UpdateUserDTO,
], ],
} }
@@ -41,7 +47,10 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
} }
// delete user // delete user
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const { id } = req.params const { id } = req.params
const workflow = deleteUsersWorkflow(req.scope) const workflow = deleteUsersWorkflow(req.scope)
@@ -1,14 +1,22 @@
import { transformBody, transformQuery } from "../../../api/middlewares" import * as QueryConfig from "./query-config"
import { import {
AdminCreateUserRequest, AdminCreateUserRequest,
AdminGetUsersParams, AdminGetUsersParams,
AdminGetUsersUserParams, AdminGetUsersUserParams,
AdminUpdateUserRequest, AdminUpdateUserRequest,
} from "./validators" } from "./validators"
import * as QueryConfig from "./query-config" import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../types/middlewares" import { MiddlewareRoute } from "../../../types/middlewares"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [ export const adminUserRoutesMiddlewares: MiddlewareRoute[] = [
{
method: ["ALL"],
matcher: "/admin/users*",
middlewares: [authenticate("admin", ["bearer", "session"])],
},
{ {
method: ["GET"], method: ["GET"],
matcher: "/admin/users", matcher: "/admin/users",
@@ -1,12 +1,19 @@
import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
import { createUsersWorkflow } from "@medusajs/core-flows"
import { CreateUserDTO } from "@medusajs/types"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { CreateUserDTO } from "@medusajs/types"
import { createUsersWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY) const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const query = remoteQueryObjectFromString({ const query = remoteQueryObjectFromString({
@@ -32,12 +39,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: AuthenticatedMedusaRequest<CreateUserDTO>,
res: MedusaResponse
) => {
const workflow = createUsersWorkflow(req.scope) const workflow = createUsersWorkflow(req.scope)
const input = { const input = {
input: { input: {
users: [req.validatedBody as CreateUserDTO], users: [req.validatedBody],
}, },
} }
@@ -1,8 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { IWorkflowEngineService } from "@medusajs/workflows-sdk" AuthenticatedMedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" MedusaResponse,
} from "../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
@@ -1,11 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { import {
MedusaRequest, AuthenticatedMedusaRequest,
MedusaResponse, MedusaResponse,
} from "../../../../../../../types/routing" } from "../../../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
@@ -1,8 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { IWorkflowEngineService } from "@medusajs/workflows-sdk" AuthenticatedMedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" MedusaResponse,
} from "../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
@@ -1,20 +1,26 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { import {
IWorkflowEngineService, IWorkflowEngineService,
WorkflowOrchestratorTypes, WorkflowOrchestratorTypes,
} from "@medusajs/workflows-sdk" } from "@medusajs/workflows-sdk"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
import { AdminPostWorkflowsRunReq } from "../../validators"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { import { AdminPostWorkflowsRunReq } from "../../validators"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostWorkflowsRunReq>,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
const { workflow_id } = req.params const { workflow_id } = req.params
const { transaction_id, input } = const { transaction_id, input } = req.validatedBody
req.validatedBody as AdminPostWorkflowsRunReq
const options = { const options = {
transactionId: transaction_id, transactionId: transaction_id,
@@ -1,17 +1,24 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { TransactionHandlerType, isDefined } from "@medusajs/utils" AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk" import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk"
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing" import { TransactionHandlerType, isDefined } from "@medusajs/utils"
import { AdminPostWorkflowsAsyncResponseReq } from "../../../validators"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { import { AdminPostWorkflowsAsyncResponseReq } from "../../../validators"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostWorkflowsAsyncResponseReq>,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
const { workflow_id } = req.params const { workflow_id } = req.params
const body = req.validatedBody as AdminPostWorkflowsAsyncResponseReq const body = req.validatedBody
const { transaction_id, step_id } = body const { transaction_id, step_id } = body
@@ -1,17 +1,24 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { TransactionHandlerType, isDefined } from "@medusajs/utils" AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../../../types/routing"
import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk" import { IWorkflowEngineService, StepResponse } from "@medusajs/workflows-sdk"
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing" import { TransactionHandlerType, isDefined } from "@medusajs/utils"
import { AdminPostWorkflowsAsyncResponseReq } from "../../../validators"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { import { AdminPostWorkflowsAsyncResponseReq } from "../../../validators"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostWorkflowsAsyncResponseReq>,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
const { workflow_id } = req.params const { workflow_id } = req.params
const body = req.validatedBody as AdminPostWorkflowsAsyncResponseReq const body = req.validatedBody
const { transaction_id, step_id } = body const { transaction_id, step_id } = body
@@ -1,8 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { IWorkflowEngineService } from "@medusajs/workflows-sdk" AuthenticatedMedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing" MedusaResponse,
} from "../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
@@ -1,14 +1,22 @@
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import { import {
AdminGetWorkflowExecutionDetailsParams, AdminGetWorkflowExecutionDetailsParams,
AdminGetWorkflowExecutionsParams, AdminGetWorkflowExecutionsParams,
AdminPostWorkflowsAsyncResponseReq, AdminPostWorkflowsAsyncResponseReq,
AdminPostWorkflowsRunReq, AdminPostWorkflowsRunReq,
} from "./validators" } from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware"
export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [ export const adminWorkflowsExecutionsMiddlewares: MiddlewareRoute[] = [
{
method: ["ALL"],
matcher: "/admin/workflows-executions*",
middlewares: [authenticate("admin", ["bearer", "session"])],
},
{ {
method: ["GET"], method: ["GET"],
matcher: "/admin/workflows-executions", matcher: "/admin/workflows-executions",
@@ -1,8 +1,15 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import {
import { IWorkflowEngineService } from "@medusajs/workflows-sdk" AuthenticatedMedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../types/routing" MedusaResponse,
} from "../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { IWorkflowEngineService } from "@medusajs/workflows-sdk"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const workflowEngineService: IWorkflowEngineService = req.scope.resolve( const workflowEngineService: IWorkflowEngineService = req.scope.resolve(
ModuleRegistrationName.WORKFLOW_ENGINE ModuleRegistrationName.WORKFLOW_ENGINE
) )
@@ -1,7 +1,13 @@
import { MedusaRequest, MedusaResponse } from "../../../types/routing" import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req.session.auth_user = req.auth_user req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
req.session.auth_user = req.auth
res.status(200).json({ user: req.auth_user }) res.status(200).json({ user: req.auth })
} }
@@ -1,9 +1,9 @@
import { updateCartsWorkflow } from "@medusajs/core-flows"
import { UpdateCartDataDTO } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { remoteQueryObjectFromString } from "@medusajs/utils" import { UpdateCartDataDTO } from "@medusajs/types"
import { defaultStoreCartFields } from "../query-config" import { defaultStoreCartFields } from "../query-config"
import { remoteQueryObjectFromString } from "@medusajs/utils"
import { updateCartsWorkflow } from "@medusajs/core-flows"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const remoteQuery = req.scope.resolve("remoteQuery") const remoteQuery = req.scope.resolve("remoteQuery")
@@ -20,12 +20,15 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.json({ cart }) res.json({ cart })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
req: MedusaRequest<UpdateCartDataDTO>,
res: MedusaResponse
) => {
const updateCartWorkflow = updateCartsWorkflow(req.scope) const updateCartWorkflow = updateCartsWorkflow(req.scope)
const workflowInput = { const workflowInput = {
selector: { id: req.params.id }, selector: { id: req.params.id },
update: req.validatedBody as UpdateCartDataDTO, update: req.validatedBody,
} }
const { result, errors } = await updateCartWorkflow.run({ const { result, errors } = await updateCartWorkflow.run({
@@ -1,15 +1,24 @@
import { createCartWorkflow } from "@medusajs/core-flows" import {
import { CreateCartWorkflowInputDTO } from "@medusajs/types" AuthenticatedMedusaRequest,
import { remoteQueryObjectFromString } from "@medusajs/utils" MedusaRequest,
import { MedusaRequest, MedusaResponse } from "../../../types/routing" MedusaResponse,
import { defaultStoreCartFields } from "../carts/query-config" } from "../../../types/routing"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { import { CreateCartWorkflowInputDTO } from "@medusajs/types"
const workflowInput = req.validatedBody as CreateCartWorkflowInputDTO import { StorePostCartReq } from "./validators"
import { createCartWorkflow } from "@medusajs/core-flows"
import { defaultStoreCartFields } from "../carts/query-config"
import { remoteQueryObjectFromString } from "@medusajs/utils"
export const POST = async (
req: AuthenticatedMedusaRequest<CreateCartWorkflowInputDTO>,
res: MedusaResponse
) => {
const workflowInput = req.validatedBody
// If the customer is logged in, we auto-assign them to the cart // If the customer is logged in, we auto-assign them to the cart
if (req.auth_user?.app_metadata?.customer_id) { if (req.auth?.actor_id) {
workflowInput.customer_id = req.auth_user!.app_metadata?.customer_id workflowInput.customer_id = req.auth.actor_id
} }
const { result, errors } = await createCartWorkflow(req.scope).run({ const { result, errors } = await createCartWorkflow(req.scope).run({
@@ -1,14 +1,22 @@
import { import {
updateCustomerAddressesWorkflow, AuthenticatedMedusaRequest,
deleteCustomerAddressesWorkflow, MedusaRequest,
} from "@medusajs/core-flows" MedusaResponse,
import { ModuleRegistrationName } from "@medusajs/modules-sdk" } from "../../../../../../types/routing"
import { CustomerAddressDTO, ICustomerModuleService } from "@medusajs/types" import { CustomerAddressDTO, ICustomerModuleService } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils" import {
import { MedusaRequest, MedusaResponse } from "../../../../../../types/routing" deleteCustomerAddressesWorkflow,
updateCustomerAddressesWorkflow,
} from "@medusajs/core-flows"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { MedusaError } from "@medusajs/utils"
const id = req.auth_user!.app_metadata.customer_id import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.auth.actor_id
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
@@ -25,8 +33,11 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ address }) res.status(200).json({ address })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
const id = req.auth_user!.app_metadata.customer_id req: AuthenticatedMedusaRequest<Partial<CustomerAddressDTO>>,
res: MedusaResponse
) => {
const id = req.auth.actor_id!
const service = req.scope.resolve<ICustomerModuleService>( const service = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
) )
@@ -37,7 +48,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const { result, errors } = await updateAddresses.run({ const { result, errors } = await updateAddresses.run({
input: { input: {
selector: { id: req.params.address_id, customer_id: req.params.id }, selector: { id: req.params.address_id, customer_id: req.params.id },
update: req.validatedBody as Partial<CustomerAddressDTO>, update: req.validatedBody,
}, },
throwOnError: false, throwOnError: false,
}) })
@@ -49,8 +60,11 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
res.status(200).json({ address: result[0] }) res.status(200).json({ address: result[0] })
} }
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => { export const DELETE = async (
const id = req.auth_user!.app_metadata.customer_id req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.auth.actor_id
const service = req.scope.resolve<ICustomerModuleService>( const service = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
@@ -1,13 +1,21 @@
import { createCustomerAddressesWorkflow } from "@medusajs/core-flows" import {
import { ModuleRegistrationName } from "@medusajs/modules-sdk" AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse,
} from "../../../../../types/routing"
import { import {
CreateCustomerAddressDTO, CreateCustomerAddressDTO,
ICustomerModuleService, ICustomerModuleService,
} from "@medusajs/types" } from "@medusajs/types"
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { import { ModuleRegistrationName } from "@medusajs/modules-sdk"
const customerId = req.auth_user!.app_metadata.customer_id import { createCustomerAddressesWorkflow } from "@medusajs/core-flows"
export const GET = async (
req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const customerId = req.auth.actor_id
const customerModuleService = req.scope.resolve<ICustomerModuleService>( const customerModuleService = req.scope.resolve<ICustomerModuleService>(
ModuleRegistrationName.CUSTOMER ModuleRegistrationName.CUSTOMER
@@ -28,13 +36,16 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
}) })
} }
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
const customerId = req.auth_user!.app_metadata.customer_id req: AuthenticatedMedusaRequest<CreateCustomerAddressDTO>,
res: MedusaResponse
) => {
const customerId = req.auth.actor_id
const createAddresses = createCustomerAddressesWorkflow(req.scope) const createAddresses = createCustomerAddressesWorkflow(req.scope)
const addresses = [ const addresses = [
{ {
...(req.validatedBody as CreateCustomerAddressDTO), ...req.validatedBody,
customer_id: customerId, customer_id: customerId,
}, },
] ]
@@ -1,9 +1,15 @@
import { MedusaRequest, MedusaResponse } from "../../../../types/routing" import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../../types/routing"
import { ModuleRegistrationName } from "@medusajs/modules-sdk" import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { export const GET = async (
const id = req.auth_user!.app_metadata?.customer_id req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
const id = req.auth.actor_id
const customerModule = req.scope.resolve(ModuleRegistrationName.CUSTOMER) const customerModule = req.scope.resolve(ModuleRegistrationName.CUSTOMER)
@@ -1,22 +1,30 @@
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import {
StorePostCustomersReq,
StoreGetCustomersMeParams,
StorePostCustomersMeAddressesReq,
StorePostCustomersMeAddressesAddressReq,
StoreGetCustomersMeAddressesParams,
} from "./validators"
import * as QueryConfig from "./query-config" import * as QueryConfig from "./query-config"
import {
StoreGetCustomersMeAddressesParams,
StoreGetCustomersMeParams,
StorePostCustomersMeAddressesAddressReq,
StorePostCustomersMeAddressesReq,
StorePostCustomersReq,
} from "./validators"
import { transformBody, transformQuery } from "../../../api/middlewares"
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
import { authenticate } from "../../../utils/authenticate-middleware" import { authenticate } from "../../../utils/authenticate-middleware"
export const storeCustomerRoutesMiddlewares: MiddlewareRoute[] = [ export const storeCustomerRoutesMiddlewares: MiddlewareRoute[] = [
{ {
method: "ALL", method: "ALL",
matcher: "/store/customers*", matcher: "/store/customers/me*",
middlewares: [authenticate("store", ["session", "bearer"])], middlewares: [authenticate("store", ["session", "bearer"])],
}, },
{
method: "POST",
matcher: "/store/customers",
middlewares: [
authenticate("store", ["session", "bearer"], { allowUnregistered: true }),
],
},
{ {
method: ["POST"], method: ["POST"],
matcher: "/store/customers", matcher: "/store/customers",
@@ -1,21 +1,27 @@
import { MedusaRequest, MedusaResponse } from "../../../types/routing" import {
AuthenticatedMedusaRequest,
MedusaResponse,
} from "../../../types/routing"
import { import {
ContainerRegistrationKeys, ContainerRegistrationKeys,
remoteQueryObjectFromString, remoteQueryObjectFromString,
} from "@medusajs/utils" } from "@medusajs/utils"
import { CreateCustomerDTO } from "@medusajs/types" import { CreateCustomerDTO } from "@medusajs/types"
import { createCustomerAccountWorkflow } from "@medusajs/core-flows" import { createCustomerAccountWorkflow } from "@medusajs/core-flows"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => { export const POST = async (
if (req.auth_user?.app_metadata?.customer_id) { req: AuthenticatedMedusaRequest,
res: MedusaResponse
) => {
if (req.auth.actor_id) {
const remoteQuery = req.scope.resolve( const remoteQuery = req.scope.resolve(
ContainerRegistrationKeys.REMOTE_QUERY ContainerRegistrationKeys.REMOTE_QUERY
) )
const query = remoteQueryObjectFromString({ const query = remoteQueryObjectFromString({
entryPoint: "customer", entryPoint: "customer",
variables: { id: req.auth_user.app_metadata.customer_id }, variables: { id: req.auth.actor_id },
fields: [], fields: [],
}) })
const [customer] = await remoteQuery(query) const [customer] = await remoteQuery(query)
@@ -29,7 +35,7 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
const customersData = req.validatedBody as CreateCustomerDTO const customersData = req.validatedBody as CreateCustomerDTO
const { result } = await createCustomers.run({ const { result } = await createCustomers.run({
input: { customersData, authUserId: req.auth_user!.id }, input: { customersData, authUserId: req.auth.auth_user_id },
}) })
// Set customer_id on session user if we are in session // Set customer_id on session user if we are in session
@@ -1,4 +1,6 @@
import { ContainerRegistrationKeys, MedusaV2Flag } from "@medusajs/utils"
import { NextFunction, Request, RequestHandler, Response } from "express" import { NextFunction, Request, RequestHandler, Response } from "express"
import passport from "passport" import passport from "passport"
// Optional customer authentication // Optional customer authentication
@@ -6,6 +8,13 @@ import passport from "passport"
// If you want to require authentication, use `requireCustomerAuthentication` in `packages/medusa/src/api/middlewares/require-customer-authentication.ts` // If you want to require authentication, use `requireCustomerAuthentication` in `packages/medusa/src/api/middlewares/require-customer-authentication.ts`
export default (): RequestHandler => { export default (): RequestHandler => {
return (req: Request, res: Response, next: NextFunction): void => { return (req: Request, res: Response, next: NextFunction): void => {
const featureFlagRouter = req.scope.resolve(
ContainerRegistrationKeys.FEATURE_FLAG_ROUTER
)
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
return next()
}
passport.authenticate( passport.authenticate(
["store-session", "store-bearer"], ["store-session", "store-bearer"],
{ session: false }, { session: false },
@@ -1,12 +1,19 @@
import { ContainerRegistrationKeys, MedusaV2Flag } from "@medusajs/utils"
import { NextFunction, Request, RequestHandler, Response } from "express" import { NextFunction, Request, RequestHandler, Response } from "express"
import passport from "passport" import passport from "passport"
export default (): RequestHandler => { export default (): RequestHandler => {
return (req: Request, res: Response, next: NextFunction): void => { return (req: Request, res: Response, next: NextFunction): void => {
passport.authenticate(["admin-session", "admin-bearer", "admin-api-token"], { session: false })( const featureFlagRouter = req.scope.resolve(
req, ContainerRegistrationKeys.FEATURE_FLAG_ROUTER
res,
next
) )
if (featureFlagRouter.isFeatureEnabled(MedusaV2Flag.key)) {
return next()
}
passport.authenticate(
["admin-session", "admin-bearer", "admin-api-token"],
{ session: false }
)(req, res, next)
} }
} }
+20 -5
View File
@@ -1,15 +1,30 @@
import type { Customer, User } from "../models"
import type { NextFunction, Request, Response } from "express" import type { NextFunction, Request, Response } from "express"
import type { Customer, User } from "../models"
import { MedusaContainer } from "@medusajs/types" import { MedusaContainer } from "@medusajs/types"
import { RequestQueryFields } from "@medusajs/types"
export interface MedusaRequest extends Request { export interface MedusaRequest<Body = unknown> extends Request {
user?: (User | Customer) & { customer_id?: string; userId?: string } validatedBody: Body
validatedQuery: RequestQueryFields & Record<string, unknown>
allowedProperties: string[]
includes?: Record<string, boolean>
errors: string[]
scope: MedusaContainer scope: MedusaContainer
session?: any session?: any
rawBody?: any rawBody?: any
requestId?: string requestId?: string
auth_user?: { id: string; app_metadata: Record<string, any>; scope: string } }
export interface AuthenticatedMedusaRequest<Body = never>
extends MedusaRequest<Body> {
user: (User | Customer) & { customer_id?: string; userId?: string } // TODO: Remove this property when v2 is released
auth: {
actor_id: string
auth_user_id: string
app_metadata: Record<string, any>
scope: string
}
} }
export type MedusaResponse = Response export type MedusaResponse = Response
@@ -17,7 +32,7 @@ export type MedusaResponse = Response
export type MedusaNextFunction = NextFunction export type MedusaNextFunction = NextFunction
export type MedusaRequestHandler = ( export type MedusaRequestHandler = (
req: MedusaRequest, req: MedusaRequest<unknown>,
res: MedusaResponse, res: MedusaResponse,
next: MedusaNextFunction next: MedusaNextFunction
) => Promise<void> | void ) => Promise<void> | void
@@ -1,8 +1,13 @@
import { MedusaRequest, MedusaResponse } from "../types/routing" import { AuthUserDTO, IUserModuleService } from "@medusajs/types"
import {
AuthenticatedMedusaRequest,
MedusaRequest,
MedusaResponse,
} from "../types/routing"
import { NextFunction, RequestHandler } from "express" import { NextFunction, RequestHandler } from "express"
import jwt, { JwtPayload } from "jsonwebtoken" import jwt, { JwtPayload } from "jsonwebtoken"
import { AuthUserDTO } from "@medusajs/types" import { StringChain } from "lodash"
import { stringEqualsOrRegexMatch } from "@medusajs/utils" import { stringEqualsOrRegexMatch } from "@medusajs/utils"
const SESSION_AUTH = "session" const SESSION_AUTH = "session"
@@ -18,7 +23,7 @@ type AuthType = "session" | "bearer"
export const authenticate = ( export const authenticate = (
authScope: string | RegExp, authScope: string | RegExp,
authType: AuthType | AuthType[], authType: AuthType | AuthType[],
options: { allowUnauthenticated?: boolean } = {} options: { allowUnauthenticated?: boolean; allowUnregistered?: boolean } = {}
): RequestHandler => { ): RequestHandler => {
return async ( return async (
req: MedusaRequest, req: MedusaRequest,
@@ -67,9 +72,24 @@ export const authenticate = (
} }
} }
if (authUser) { const isMedusaScope =
req.auth_user = { stringEqualsOrRegexMatch(authScope, "admin") ||
id: authUser.id, stringEqualsOrRegexMatch(authScope, "store")
const isRegistered =
!isMedusaScope ||
(authUser?.app_metadata?.user_id &&
stringEqualsOrRegexMatch(authScope, "admin")) ||
(authUser?.app_metadata?.customer_id &&
stringEqualsOrRegexMatch(authScope, "store"))
if (
authUser &&
(isRegistered || (!isRegistered && options.allowUnregistered))
) {
;(req as AuthenticatedMedusaRequest).auth = {
actor_id: getActorId(authUser, authScope) as string, // TODO: fix types for auth_users not in the medusa system
auth_user_id: authUser.id,
app_metadata: authUser.app_metadata, app_metadata: authUser.app_metadata,
scope: authUser.scope, scope: authUser.scope,
} }
@@ -83,3 +103,18 @@ export const authenticate = (
res.status(401).json({ message: "Unauthorized" }) res.status(401).json({ message: "Unauthorized" })
} }
} }
const getActorId = (
authUser: AuthUserDTO,
scope: string | RegExp
): string | undefined => {
if (stringEqualsOrRegexMatch(scope, "admin")) {
return authUser.app_metadata.user_id as string
}
if (stringEqualsOrRegexMatch(scope, "store")) {
return authUser.app_metadata.customer_id as string
}
return undefined
}
@@ -1,13 +1,14 @@
import { BaseFilterable } from "../../dal"
import { CreateCampaignDTO } from "../mutations"
import { import {
ApplicationMethodDTO, ApplicationMethodDTO,
CreateApplicationMethodDTO, CreateApplicationMethodDTO,
UpdateApplicationMethodDTO, UpdateApplicationMethodDTO,
} from "./application-method" } from "./application-method"
import { CampaignDTO } from "./campaign"
import { CreatePromotionRuleDTO, PromotionRuleDTO } from "./promotion-rule" import { CreatePromotionRuleDTO, PromotionRuleDTO } from "./promotion-rule"
import { BaseFilterable } from "../../dal"
import { CampaignDTO } from "./campaign"
import { CreateCampaignDTO } from "../mutations"
export type PromotionTypeValues = "standard" | "buyget" export type PromotionTypeValues = "standard" | "buyget"
export interface PromotionDTO { export interface PromotionDTO {
+12 -2
View File
@@ -4,9 +4,10 @@ import {
MedusaError, MedusaError,
ModulesSdkUtils, ModulesSdkUtils,
} from "@medusajs/utils" } from "@medusajs/utils"
import jwt, { JwtPayload } from "jsonwebtoken"
import { Invite } from "@models" import { Invite } from "@models"
import { InviteServiceTypes } from "@types" import { InviteServiceTypes } from "@types"
import jwt, { JwtPayload } from "jsonwebtoken"
type InjectedDependencies = { type InjectedDependencies = {
inviteRepository: DAL.RepositoryService inviteRepository: DAL.RepositoryService
@@ -91,7 +92,16 @@ export default class InviteService<
): Promise<TEntity> { ): Promise<TEntity> {
const decoded = this.validateToken(token) const decoded = this.validateToken(token)
return await super.retrieve(decoded.payload.id, {}, context) const invite = await super.retrieve(decoded.payload.id, {}, context)
if (invite.expires_at < new Date()) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"The invite has expired"
)
}
return invite
} }
private generateToken(data: any): string { private generateToken(data: any): string {
+1
View File
@@ -5,4 +5,5 @@ export const ContainerRegistrationKeys = {
LOGGER: "logger", LOGGER: "logger",
REMOTE_QUERY: "remoteQuery", REMOTE_QUERY: "remoteQuery",
REMOTE_LINK: "remoteLink", REMOTE_LINK: "remoteLink",
FEATURE_FLAG_ROUTER: "featureFlagRouter",
} }