medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
26.23k stars 2.66k forks source link

[CORE-1123] Validate input to order creation #3119

Closed finom closed 12 months ago

finom commented 1 year ago

Bug report

Describe the bug

I'm trying to complete an order with the following code:

const { cart } = await medusa.carts.createPaymentSessions(medusaCartId)
await medusa.carts.setPaymentSession(medusaCartId, {
        provider_id: cart.payment_sessions[0].provider_id, // manual
})
const { type, data } = await medusa.carts.complete(medusaCartId);

But the last line throws an error.

If I call medusa.carts.complete with the same cart ID again I get

Any thoughts how to fix that? Thank you!

System information

Medusa version (including plugins): 1.6.0 Node.js version: 18.3.0 Database: Postgres Operating system: Heroku (I think Linux) Browser (if relevant): Not relevant

Steps to reproduce the behavior

See description.

Expected behavior

An order should be placed.

UPD That's what server-side log shows:

{
    "level": "error",
    "message": "null value in column \"customer_id\" of relation \"order\" violates not-null constraint",
    "stack": [{
        "columnNumber": 28,
        "fileName": "/app/node_modules/typeorm/error/TypeORMError.js",
        "functionName": "QueryFailedError.TypeORMError [as constructor]",
        "lineNumber": 9,
        "methodName": "TypeORMError [as constructor]",
        "native": false,
        "typeName": "QueryFailedError"
    }, {
        "columnNumber": 28,
        "fileName": "/app/node_modules/typeorm/error/QueryFailedError.js",
        "functionName": "new QueryFailedError",
        "lineNumber": 13,
        "methodName": null,
        "native": false,
        "typeName": null
    }, {
        "columnNumber": 31,
        "fileName": "/app/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js",
        "functionName": null,
        "lineNumber": 281,
        "methodName": null,
        "native": false,
        "typeName": "PostgresQueryRunner"
    }, {
        "columnNumber": 27,
        "fileName": "/app/node_modules/tslib/tslib.js",
        "functionName": "step",
        "lineNumber": 144,
        "methodName": null,
        "native": false,
        "typeName": null
    }, {
        "columnNumber": 57,
        "fileName": "/app/node_modules/tslib/tslib.js",
        "functionName": "Object.throw",
        "lineNumber": 125,
        "methodName": "throw",
        "native": false,
        "typeName": "Object"
    }, {
        "columnNumber": 69,
        "fileName": "/app/node_modules/tslib/tslib.js",
        "functionName": "rejected",
        "lineNumber": 116,
        "methodName": null,
        "native": false,
        "typeName": null
    }, {
        "columnNumber": 5,
        "fileName": "node:internal/process/task_queues",
        "functionName": "process.processTicksAndRejections",
        "lineNumber": 95,
        "methodName": "processTicksAndRejections",
        "native": false,
        "typeName": "process"
    }],
    "timestamp": "2023-01-26 19:45:00"
}

From SyncLinear.com | CORE-1123

finom commented 1 year ago

I found out how to fix that. I needed to set email:

await medusa.carts.update(currentUser.medusaCartId, {
      email
});

But I don't close the issue because I think the API should return a proper error instead of the "unknown error".

olivermrbl commented 1 year ago

Thanks for filing this issue @finom. We should indeed validate the order data and throw a more descriptive error. I've added it to our backlog :)