Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

2.8 KB
Raw Blame
// Copied from integration-tests/helpers/create-seller-user.ts so the e2e stack stays
// self-contained (no cross-folder imports). Update if the source drifts.
import {
    IAuthModuleService,
    MedusaContainer,
} from "@medusajs/framework/types"
import {
    ContainerRegistrationKeys,
    Modules,
} from "@medusajs/framework/utils"
import Scrypt from "scrypt-kdf"
import { createSellerAccountWorkflow } from "@mercurjs/core/workflows"

export const vendorHeaders = {
    headers: { "x-medusa-access-token": "test_token" },
}

export const createSellerUser = async (
    container: MedusaContainer,
    options?: { email?: string; name?: string }
) => {
    const email = options?.email ?? "[email protected]"
    const name = options?.name ?? "Test Seller"

    const authModule: IAuthModuleService = container.resolve(Modules.AUTH)

    // Low scrypt work factor: this is throwaway test data, and the helper runs
    // on every seller-creating spec. scrypt-kdf embeds the params in the hash,
    // so emailpass login still verifies against the cheaper cost factor.
    const hashConfig = { logN: 4, r: 8, p: 1 }
    const passwordHash = await Scrypt.kdf("somepassword", hashConfig)

    const authIdentity = await authModule.createAuthIdentities({
        provider_identities: [
            {
                provider: "emailpass",
                entity_id: email,
                provider_metadata: {
                    password: passwordHash.toString("base64"),
                },
            },
        ],
    })

    const { result: seller } = await createSellerAccountWorkflow(container).run({
        input: {
            auth_identity_id: authIdentity.id,
            member_email: email,
            seller: {
                name,
                email,
                currency_code: "usd",
            },
        },
    })

    const query = container.resolve(ContainerRegistrationKeys.QUERY)

    const { data: members } = await query.graph({
        entity: "member",
        fields: ["id"],
        filters: { email },
    })

    const member = members[0]

    const config = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE)
    const { projectConfig } = config
    const { jwtSecret, jwtOptions } = projectConfig.http
    const token = (await import("jsonwebtoken")).default.sign(
        {
            actor_id: member.id,
            actor_type: "member",
            auth_identity_id: authIdentity.id,
        },
        jwtSecret as string,
        {
            expiresIn: "1d",
            ...jwtOptions,
        }
    )

    vendorHeaders.headers["authorization"] = `Bearer ${token}`
    vendorHeaders.headers["x-seller-id"] = (seller as any).id

    const headers = {
        headers: {
            authorization: `Bearer ${token}`,
            "x-seller-id": (seller as any).id,
        },
    }

    return { seller, member, authIdentity, headers }
}