Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa, types, core-flows): Add invite endpoints for api-v2 #6395

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"

import path from "path"
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../environment-helpers/use-api"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("POST /admin/invites", () => {
let dbConnection
let shutdownServer

beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd, env } as any)
shutdownServer = await startBootstrapApp({ cwd, env })
})

beforeEach(async () => {
await adminSeeder(dbConnection)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("create an invite", async () => {
const api = useApi()! as AxiosInstance

const body = {
email: "test_member@test.com",
}

const response = await api.post(`/admin/invites`, body, adminHeaders)

expect(response.status).toEqual(200)
expect(response.data).toEqual({
invite: expect.objectContaining(body),
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"

import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
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"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("DELETE /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 adminSeeder(dbConnection)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should delete a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
})

const api = useApi()! as AxiosInstance

const response = await api.delete(
`/admin/invites/${invite.id}`,
adminHeaders
)

expect(response.status).toEqual(200)
expect(response.data).toEqual({
id: invite.id,
object: "invite",
deleted: true,
})

const { response: deletedResponse } = await api
.get(`/admin/invites/${invite.id}`, adminHeaders)
.catch((e) => e)

expect(deletedResponse.status).toEqual(404)
expect(deletedResponse.data.type).toEqual("not_found")
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"

import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
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"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"

jest.setTimeout(50000)

const env = { MEDUSA_FF_MEDUSA_V2: true }
const adminHeaders = {
headers: { "x-medusa-access-token": "test_token" },
}

describe("GET /admin/invites", () => {
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 adminSeeder(dbConnection)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should list invites", async () => {
await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
})

const api = useApi()! as AxiosInstance

const response = await api.get(`/admin/invites`, adminHeaders)

expect(response.status).toEqual(200)
expect(response.data).toEqual({
invites: [
expect.objectContaining({ email: "potential_member@test.com" }),
],
count: 1,
offset: 0,
limit: 50,
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { initDb, useDb } from "../../../environment-helpers/use-db"

import { IUserModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
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"
import adminSeeder from "../../../helpers/admin-seeder"
import { AxiosInstance } from "axios"

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 adminSeeder(dbConnection)
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
await shutdownServer()
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should retrieve a single invite", async () => {
const invite = await userModuleService.createInvites({
email: "potential_member@test.com",
token: "test",
expires_at: new Date(),
})

const api = useApi()! as AxiosInstance

const response = await api.get(`/admin/invites/${invite.id}`, adminHeaders)

expect(response.status).toEqual(200)
expect(response.data.invite).toEqual(
expect.objectContaining({ email: "potential_member@test.com" })
)
})
})
1 change: 1 addition & 0 deletions packages/core-flows/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./promotion"
export * from "./customer"
export * from "./customer-group"
export * from "./user"
export * from "./invite"
2 changes: 2 additions & 0 deletions packages/core-flows/src/invite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./steps"
export * from "./workflows"
31 changes: 31 additions & 0 deletions packages/core-flows/src/invite/steps/create-invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { CreateInviteDTO, IUserModuleService, InviteDTO } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"

export const createInviteStepId = "create-invite-step"
export const createInviteStep = createStep(
createInviteStepId,
async (input: CreateInviteDTO[], { container }) => {
const service: IUserModuleService = container.resolve(
ModuleRegistrationName.USER
)

const createdInvites = await service.createInvites(input)

return new StepResponse(
createdInvites,
createdInvites.map((inv) => inv.id)
)
},
async (createdInvitesIds, { container }) => {
if (!createdInvitesIds?.length) {
return
}

const service: IUserModuleService = container.resolve(
ModuleRegistrationName.USER
)

await service.deleteInvites(createdInvitesIds)
}
)
28 changes: 28 additions & 0 deletions packages/core-flows/src/invite/steps/delete-invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IUserModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"

export const deleteInvitesStepId = "delete-invites-step"
export const deleteInvitesStep = createStep(
deleteInvitesStepId,
async (input: string[], { container }) => {
const service: IUserModuleService = container.resolve(
ModuleRegistrationName.USER
)

await service.softDeleteInvites(input)
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved

return new StepResponse(void 0, input)
},
async (deletedInviteIds, { container }) => {
if (!deletedInviteIds?.length) {
return
}

const service: IUserModuleService = container.resolve(
ModuleRegistrationName.USER
)

await service.restoreInvites(deletedInviteIds)
}
)
2 changes: 2 additions & 0 deletions packages/core-flows/src/invite/steps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./create-invites"
export * from "./delete-invites"
13 changes: 13 additions & 0 deletions packages/core-flows/src/invite/workflows/create-invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createInviteStep } from "../steps"
import { InviteDTO, InviteWorkflow } from "@medusajs/types"

export const createInvitesWorkflowId = "create-invite-step"
export const createInvitesWorkflow = createWorkflow(
createInvitesWorkflowId,
(
input: WorkflowData<InviteWorkflow.CreateInvitesWorkflowInputDTO>
): WorkflowData<InviteDTO[]> => {
return createInviteStep(input.invites)
}
)
13 changes: 13 additions & 0 deletions packages/core-flows/src/invite/workflows/delete-invites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { deleteInvitesStep } from "../steps"
import { InviteWorkflow, UserWorkflow } from "@medusajs/types"

export const deleteInvitesWorkflowId = "delete-invites-workflow"
export const deleteInvitesWorkflow = createWorkflow(
deleteInvitesWorkflowId,
(
input: WorkflowData<InviteWorkflow.DeleteInvitesWorkflowInput>
): WorkflowData<void> => {
return deleteInvitesStep(input.ids)
}
)
2 changes: 2 additions & 0 deletions packages/core-flows/src/invite/workflows/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./create-invites"
export * from "./delete-invites"
Loading
Loading