Skip to content

Commit

Permalink
Merge branch 'develop' into docs/fix-config-title
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser authored Jan 31, 2024
2 parents 6bc7e8c + e749dd6 commit bf2013f
Show file tree
Hide file tree
Showing 117 changed files with 5,308 additions and 419 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-spies-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/types": patch
---

feat(types): added computed actions for automatic promotions
46 changes: 33 additions & 13 deletions docs-util/packages/docblock-generator/src/classes/git-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Octokit } from "octokit"
import promiseExec from "../utils/promise-exec.js"
import getMonorepoRoot from "../utils/get-monorepo-root.js"
import filterFiles from "../utils/filter-files.js"

type Options = {
owner?: string
Expand Down Expand Up @@ -52,24 +55,41 @@ export class GitManager {

await Promise.all(
commits.map(async (commit) => {
const {
data: { files: commitFiles },
} = await this.octokit.request(
"GET /repos/{owner}/{repo}/commits/{ref}",
{
owner: this.owner,
repo: this.repo,
ref: commit.sha,
headers: {
"X-GitHub-Api-Version": this.gitApiVersion,
},
}
)
const commitFiles = await this.getCommitFiles(commit.sha)

commitFiles?.forEach((commitFile) => files.add(commitFile.filename))
})
)

return [...files]
}

async getDiffFiles(): Promise<string[]> {
const childProcess = await promiseExec(
`git diff --name-only -- "packages/**/**.ts" "packages/**/*.js" "packages/**/*.tsx" "packages/**/*.jsx"`,
{
cwd: getMonorepoRoot(),
}
)

return filterFiles(
childProcess.stdout.toString().split("\n").filter(Boolean)
)
}

async getCommitFiles(commitSha: string) {
const {
data: { files },
} = await this.octokit.request("GET /repos/{owner}/{repo}/commits/{ref}", {
owner: "medusajs",
repo: "medusa",
ref: commitSha,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
per_page: 3000,
})

return files
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import path from "path"
import DocblockGenerator from "../classes/docblock-generator.js"
import getMonorepoRoot from "../utils/get-monorepo-root.js"
import promiseExec from "../utils/promise-exec.js"
import filterFiles from "../utils/filter-files.js"
import { GitManager } from "../classes/git-manager.js"

export default async function runGitChanges() {
const monorepoPath = getMonorepoRoot()
// retrieve the changed files under `packages` in the monorepo root.
const childProcess = await promiseExec(
`git diff --name-only -- "packages/**/**.ts" "packages/**/*.js" "packages/**/*.tsx" "packages/**/*.jsx"`,
{
cwd: monorepoPath,
}
)

let files = filterFiles(
childProcess.stdout.toString().split("\n").filter(Boolean)
)
const gitManager = new GitManager()
let files = await gitManager.getDiffFiles()

if (!files.length) {
console.log(`No file changes detected.`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import { Octokit } from "@octokit/core"
import filterFiles from "../utils/filter-files.js"
import path from "path"
import getMonorepoRoot from "../utils/get-monorepo-root.js"
import DocblockGenerator from "../classes/docblock-generator.js"
import { GitManager } from "../classes/git-manager.js"

export default async function (commitSha: string) {
const monorepoPath = getMonorepoRoot()
// retrieve the files changed in the commit
const octokit = new Octokit({
auth: process.env.GH_TOKEN,
})
const gitManager = new GitManager()

const {
data: { files },
} = await octokit.request("GET /repos/{owner}/{repo}/commits/{ref}", {
owner: "medusajs",
repo: "medusa",
ref: commitSha,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
})
const files = await gitManager.getCommitFiles(commitSha)

// filter changed files
let filteredFiles = filterFiles(files?.map((file) => file.filename) || [])
Expand Down
73 changes: 73 additions & 0 deletions integration-tests/plugins/__tests__/cart/store/get-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICartModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
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)

const env = { MEDUSA_FF_MEDUSA_V2: true }

describe("GET /store/:id", () => {
let dbConnection
let appContainer
let shutdownServer
let cartModuleService: ICartModuleService

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

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

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

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

it("should get cart", async () => {
const cart = await cartModuleService.create({
currency_code: "usd",
items: [
{
unit_price: 1000,
quantity: 1,
title: "Test item",
},
],
})

const api = useApi() as any
const response = await api.get(`/store/carts/${cart.id}`)

expect(response.status).toEqual(200)
expect(response.data.cart).toEqual(
expect.objectContaining({
id: cart.id,
currency_code: "usd",
items: expect.arrayContaining([
expect.objectContaining({
unit_price: 1000,
quantity: 1,
title: "Test item",
}),
]),
})
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
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"

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

describe("POST /admin/customer-groups/:id/customers/batch", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

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

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

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

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

it("should batch add customers to a group", async () => {
const api = useApi() as any

const group = await customerModuleService.createCustomerGroup({
name: "VIP",
})
const customers = await customerModuleService.create([
{
first_name: "Test",
last_name: "Test",
},
{
first_name: "Test2",
last_name: "Test2",
},
{
first_name: "Test3",
last_name: "Test3",
},
])

const response = await api.post(
`/admin/customer-groups/${group.id}/customers/batch`,
{
customer_ids: customers.map((c) => ({ id: c.id })),
},
adminHeaders
)

expect(response.status).toEqual(200)

const updatedGroup = await customerModuleService.retrieveCustomerGroup(
group.id,
{
relations: ["customers"],
}
)
expect(updatedGroup.customers?.length).toEqual(3)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService } from "@medusajs/types"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
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"

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

describe("DELETE /admin/customer-groups/:id/customers/remove", () => {
let dbConnection
let appContainer
let shutdownServer
let customerModuleService: ICustomerModuleService

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

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

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

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

it("should batch delete customers from a group", async () => {
const api = useApi() as any

const group = await customerModuleService.createCustomerGroup({
name: "VIP",
})
const customers = await customerModuleService.create([
{
first_name: "Test",
last_name: "Test",
},
{
first_name: "Test2",
last_name: "Test2",
},
{
first_name: "Test3",
last_name: "Test3",
},
])

await customerModuleService.addCustomerToGroup(
customers.map((c) => ({ customer_id: c.id, customer_group_id: group.id }))
)

const response = await api.post(
`/admin/customer-groups/${group.id}/customers/remove`,
{
customer_ids: customers.map((c) => ({ id: c.id })),
},
adminHeaders
)

expect(response.status).toEqual(200)

const updatedGroup = await customerModuleService.retrieveCustomerGroup(
group.id,
{
relations: ["customers"],
}
)
expect(updatedGroup.customers?.length).toEqual(0)
})
})
Loading

0 comments on commit bf2013f

Please sign in to comment.