npm package: @shopnex/cj-plugin
Description: A blank template to get started with Payload 3.0
Author: ShopNex
Latest version: 1.0.10

# CJ Dropshipping Plugin for Payload CMS

This plugin integrates **CJ Dropshipping** into your **Payload CMS** project by:

- Adding "CJ" as a product source.
- Automatically creating CJ orders after local orders are created.
- (Optionally) generating a `cj-settings` collection for managing API credentials dynamically.
- Syncing CJ credentials on app initialization.

---

## Installation

```bash
pnpm install @shopnex/cj-plugin
```

or

```bash
npm install @shopnex/cj-plugin
```

or

```bash
yarn add @shopnex/cj-plugin
```

---

## Usage

Import and add the `cjPlugin` to your Payload config:

```ts
import { buildConfig } from "payload/config";
import { cjPlugin } from "@shopnex/cj-plugin";

export default buildConfig({
    collections: [
        // your collections
    ],
    plugins: [
        cjPlugin({
            cjApiKey: "your-cj-api-key",
            cjEmailAddress: "your-email@example.com",
            cjRefreshToken: "optional-refresh-token", // optional
            asCollection: true, // optional - creates a cj-settings collection
            collectionOverrides: {
                // optional - override fields or settings for the cj-settings collection
            },
        }),
    ],
});
```

---

## Plugin Options

| Option                | Type      | Required | Description                                                                                             |
| --------------------- | --------- | -------- | ------------------------------------------------------------------------------------------------------- |
| `cjApiKey`            | `string`  | Yes      | Your CJ Dropshipping API key.                                                                           |
| `cjEmailAddress`      | `string`  | Yes      | Your CJ account email address.                                                                          |
| `cjRefreshToken`      | `string`  | No       | Your CJ refresh token if available.                                                                     |
| `asCollection`        | `boolean` | No       | If true, automatically creates a `cj-settings` collection to manage CJ API credentials per tenant/shop. |
| `collectionOverrides` | `object`  | No       | Allows overriding the default `cj-settings` collection config (fields, hooks, etc).                     |

---

## What the Plugin Does

- **Modifies the `products` collection**:
    - Adds a `source` option: `{ label: 'CJ', value: 'cj' }`.
- **Hooks into the `orders` collection**:
    - On every new order or update, triggers the CJ order creation logic automatically.
- **Manages CJ credentials dynamically**:
    - On Payload startup (`onInit`), loads all `cj-settings` documents and sets up credentials per shop.
- **Optional `cj-settings` collection**:
    - If `asCollection` is true, a new `cj-settings` collection will be added automatically.
    - Useful for multi-tenant setups or dynamically changing credentials.

---

## Example: cj-settings Collection

If `asCollection: true`, the following collection will be generated by default:

| Field      | Type     | Description                       |
| ---------- | -------- | --------------------------------- |
| `shop`     | Relation | Reference to your shop or tenant. |
| `email`    | Text     | Your CJ email address.            |
| `apiToken` | Text     | Your CJ API key.                  |

You can override this structure with `collectionOverrides`.

---

## Notes

- The plugin assumes you have a `products` collection with a `source` select field already created.
- It assumes you have an `orders` collection.
- Make sure the `source` field in `products` uses a `select` type with `options` array.

---

## License

MIT