From 7fd22ecb4d5190e92c6750a9fbf2d8534bb9f4ab Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Thu, 4 May 2023 17:25:48 +0200 Subject: [PATCH] feat(client-types, types, medusa, inventory): Inventory item and reservation item datamodel updates (#3971) * add fields * add title in migration * update api endpoints to reflect datamodel changes * update migration exports for inventory module * add changeset * add created_by for reservation item --- .changeset/itchy-ligers-enjoy.md | 8 +++++ .../src/lib/models/InventoryItemDTO.ts | 12 +++++++ .../src/lib/models/ReservationItemDTO.ts | 8 +++++ packages/inventory/src/migrations/index.ts | 5 +-- ...7363119-item_descriptions_and_thumbnail.ts | 33 +++++++++++++++++++ .../inventory/src/models/inventory-item.ts | 11 ++++++- .../inventory/src/models/reservation-item.ts | 8 ++++- .../inventory/src/services/inventory-item.ts | 3 ++ .../src/services/reservation-item.ts | 2 ++ .../inventory-items/create-inventory-item.ts | 21 +++++++++--- .../transaction/create-inventory-item.ts | 23 ++++++++----- .../inventory-items/update-inventory-item.ts | 15 ++++++++- .../admin/reservations/create-reservation.ts | 16 ++++++--- .../admin/reservations/update-reservation.ts | 9 +++-- packages/types/src/inventory/common.ts | 30 ++++++++++++++++- 15 files changed, 180 insertions(+), 24 deletions(-) create mode 100644 .changeset/itchy-ligers-enjoy.md create mode 100644 packages/inventory/src/migrations/schema-migrations/1682927363119-item_descriptions_and_thumbnail.ts diff --git a/.changeset/itchy-ligers-enjoy.md b/.changeset/itchy-ligers-enjoy.md new file mode 100644 index 0000000000..5b9526a39e --- /dev/null +++ b/.changeset/itchy-ligers-enjoy.md @@ -0,0 +1,8 @@ +--- +"@medusajs/client-types": patch +"@medusajs/inventory": patch +"@medusajs/medusa": patch +"@medusajs/types": patch +--- + +Feat(client-types, inventory, medusa, types): add `title`, `thumbnail` and `description to inventory item and `description` to reservation item. diff --git a/packages/generated/client-types/src/lib/models/InventoryItemDTO.ts b/packages/generated/client-types/src/lib/models/InventoryItemDTO.ts index 2ca80cef4e..7978a3ecb5 100644 --- a/packages/generated/client-types/src/lib/models/InventoryItemDTO.ts +++ b/packages/generated/client-types/src/lib/models/InventoryItemDTO.ts @@ -20,6 +20,18 @@ export interface InventoryItemDTO { * The Manufacturers Identification code that identifies the manufacturer of the Inventory Item. May be used by Fulfillment Providers to pass customs information to shipping carriers. */ mid_code?: string + /** + * Title of the inventory item + */ + title?: string + /** + * Description of the inventory item + */ + description?: string + /** + * Thumbnail for the inventory item + */ + thumbnail?: string /** * The material and composition that the Inventory Item is made of, May be used by Fulfillment Providers to pass customs information to shipping carriers. */ diff --git a/packages/generated/client-types/src/lib/models/ReservationItemDTO.ts b/packages/generated/client-types/src/lib/models/ReservationItemDTO.ts index 39b46e6e42..485baa0353 100644 --- a/packages/generated/client-types/src/lib/models/ReservationItemDTO.ts +++ b/packages/generated/client-types/src/lib/models/ReservationItemDTO.ts @@ -19,6 +19,14 @@ export interface ReservationItemDTO { * The id of the inventory item the reservation relates to */ inventory_item_id: string + /** + * Description of the reservation item + */ + description?: string + /** + * UserId of user who created the reservation item + */ + created_by?: string /** * The id of the reservation item */ diff --git a/packages/inventory/src/migrations/index.ts b/packages/inventory/src/migrations/index.ts index d08cd39512..67c160391f 100644 --- a/packages/inventory/src/migrations/index.ts +++ b/packages/inventory/src/migrations/index.ts @@ -1,4 +1,5 @@ -import * as setup from "./schema-migrations/1665748086258-inventory_setup" import * as addExternalId from "./schema-migrations/1675761451145-add_reservation_external_id" +import * as descriptionsAndThumbnail from "./schema-migrations/1682927363119-item_descriptions_and_thumbnail" +import * as setup from "./schema-migrations/1665748086258-inventory_setup" -export default [setup, addExternalId] +export default [setup, addExternalId, descriptionsAndThumbnail] diff --git a/packages/inventory/src/migrations/schema-migrations/1682927363119-item_descriptions_and_thumbnail.ts b/packages/inventory/src/migrations/schema-migrations/1682927363119-item_descriptions_and_thumbnail.ts new file mode 100644 index 0000000000..9afa05e419 --- /dev/null +++ b/packages/inventory/src/migrations/schema-migrations/1682927363119-item_descriptions_and_thumbnail.ts @@ -0,0 +1,33 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class itemDescriptionsAndThumbnail1682927363119 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + ALTER TABLE inventory_item + ADD "title" character varying, + ADD "description" character varying, + ADD "thumbnail" character varying; + `) + await queryRunner.query(` + ALTER TABLE "reservation_item" + ADD "description" character varying, + ADD "created_by" character varying; + `) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + ALTER TABLE reservation_item + DROP COLUMN "title", + DROP COLUMN "description", + DROP COLUMN "thumbnail"; + `) + await queryRunner.query(` + ALTER TABLE "reservation_item" + DROP COLUMN "description", + DROP COLUMN "created_by"; + `) + } +} diff --git a/packages/inventory/src/models/inventory-item.ts b/packages/inventory/src/models/inventory-item.ts index a055345d35..c802cf9861 100644 --- a/packages/inventory/src/models/inventory-item.ts +++ b/packages/inventory/src/models/inventory-item.ts @@ -1,5 +1,5 @@ -import { generateEntityId, SoftDeletableEntity } from "@medusajs/utils" import { BeforeInsert, Column, Entity, Index } from "typeorm" +import { SoftDeletableEntity, generateEntityId } from "@medusajs/utils" @Entity() export class InventoryItem extends SoftDeletableEntity { @@ -34,6 +34,15 @@ export class InventoryItem extends SoftDeletableEntity { @Column({ default: true }) requires_shipping: boolean + @Column({ type: "text", nullable: true }) + description: string | null + + @Column({ type: "text", nullable: true }) + title: string | null + + @Column({ type: "text", nullable: true }) + thumbnail: string | null + @Column({ type: "jsonb", nullable: true }) metadata: Record | null diff --git a/packages/inventory/src/models/reservation-item.ts b/packages/inventory/src/models/reservation-item.ts index f84b9548f4..eece34accd 100644 --- a/packages/inventory/src/models/reservation-item.ts +++ b/packages/inventory/src/models/reservation-item.ts @@ -1,5 +1,5 @@ -import { generateEntityId, SoftDeletableEntity } from "@medusajs/utils" import { BeforeInsert, Column, Entity, Index } from "typeorm" +import { SoftDeletableEntity, generateEntityId } from "@medusajs/utils" @Entity() export class ReservationItem extends SoftDeletableEntity { @@ -21,6 +21,12 @@ export class ReservationItem extends SoftDeletableEntity { @Column({ type: "text", nullable: true }) external_id: string | null + @Column({ type: "text", nullable: true }) + description: string | null + + @Column({ type: "text", nullable: true }) + created_by: string | null + @Column({ type: "jsonb", nullable: true }) metadata: Record | null diff --git a/packages/inventory/src/services/inventory-item.ts b/packages/inventory/src/services/inventory-item.ts index 5cf9aba7d1..0324523037 100644 --- a/packages/inventory/src/services/inventory-item.ts +++ b/packages/inventory/src/services/inventory-item.ts @@ -135,6 +135,9 @@ export default class InventoryItemService { height: data.height, width: data.width, requires_shipping: data.requires_shipping, + description: data.description, + thumbnail: data.thumbnail, + title: data.title, }) const result = await itemRepository.save(inventoryItem) diff --git a/packages/inventory/src/services/reservation-item.ts b/packages/inventory/src/services/reservation-item.ts index 568c530d4a..3ae17e9e55 100644 --- a/packages/inventory/src/services/reservation-item.ts +++ b/packages/inventory/src/services/reservation-item.ts @@ -141,6 +141,8 @@ export default class ReservationItemService { quantity: data.quantity, metadata: data.metadata, external_id: data.external_id, + description: data.description, + created_by: data.created_by, }) const [newReservationItem] = await Promise.all([ diff --git a/packages/medusa/src/api/routes/admin/inventory-items/create-inventory-item.ts b/packages/medusa/src/api/routes/admin/inventory-items/create-inventory-item.ts index dcbcf8c43f..9ff125ccf0 100644 --- a/packages/medusa/src/api/routes/admin/inventory-items/create-inventory-item.ts +++ b/packages/medusa/src/api/routes/admin/inventory-items/create-inventory-item.ts @@ -1,14 +1,15 @@ -import { IInventoryService } from "@medusajs/types" -import { MedusaError } from "@medusajs/utils" import { IsNumber, IsObject, IsOptional, IsString } from "class-validator" -import { EntityManager } from "typeorm" import { ProductVariantInventoryService, ProductVariantService, } from "../../../../services" + +import { EntityManager } from "typeorm" import { FindParams } from "../../../../types/common" -import { validator } from "../../../../utils/validator" +import { IInventoryService } from "@medusajs/types" +import { MedusaError } from "@medusajs/utils" import { createInventoryItemTransaction } from "./transaction/create-inventory-item" +import { validator } from "../../../../utils/validator" /** * @oas [post] /admin/inventory-items @@ -221,6 +222,18 @@ export class AdminPostInventoryItemsReq { @IsOptional() material?: string + @IsString() + @IsOptional() + title?: string + + @IsString() + @IsOptional() + description?: string + + @IsString() + @IsOptional() + thumbnail?: string + @IsObject() @IsOptional() metadata?: Record diff --git a/packages/medusa/src/api/routes/admin/inventory-items/transaction/create-inventory-item.ts b/packages/medusa/src/api/routes/admin/inventory-items/transaction/create-inventory-item.ts index 38e770f4b4..92b02d1ce6 100644 --- a/packages/medusa/src/api/routes/admin/inventory-items/transaction/create-inventory-item.ts +++ b/packages/medusa/src/api/routes/admin/inventory-items/transaction/create-inventory-item.ts @@ -1,11 +1,3 @@ -import { IInventoryService, InventoryItemDTO } from "@medusajs/types" -import { MedusaError } from "@medusajs/utils" -import { EntityManager } from "typeorm" -import { ulid } from "ulid" -import { - ProductVariantInventoryService, - ProductVariantService, -} from "../../../../../services" import { DistributedTransaction, TransactionHandlerType, @@ -14,6 +6,15 @@ import { TransactionState, TransactionStepsDefinition, } from "../../../../../utils/transaction" +import { IInventoryService, InventoryItemDTO } from "@medusajs/types" +import { + ProductVariantInventoryService, + ProductVariantService, +} from "../../../../../services" + +import { EntityManager } from "typeorm" +import { MedusaError } from "@medusajs/utils" +import { ulid } from "ulid" enum actions { createInventoryItem = "createInventoryItem", @@ -53,6 +54,9 @@ type CreateInventoryItemInput = { origin_country?: string mid_code?: string material?: string + title?: string + description?: string + thumbnail?: string metadata?: Record } @@ -84,6 +88,9 @@ export const createInventoryItemTransaction = async ( length: input.length, height: input.height, width: input.width, + title: input.title, + description: input.description, + thumbnail: input.thumbnail, }) } diff --git a/packages/medusa/src/api/routes/admin/inventory-items/update-inventory-item.ts b/packages/medusa/src/api/routes/admin/inventory-items/update-inventory-item.ts index 807226cfd7..b8b4c365f0 100644 --- a/packages/medusa/src/api/routes/admin/inventory-items/update-inventory-item.ts +++ b/packages/medusa/src/api/routes/admin/inventory-items/update-inventory-item.ts @@ -1,8 +1,9 @@ -import { IInventoryService } from "@medusajs/types" import { IsBoolean, IsNumber, IsOptional, IsString } from "class-validator" import { Request, Response } from "express" + import { EntityManager } from "typeorm" import { FindParams } from "../../../../types/common" +import { IInventoryService } from "@medusajs/types" /** * @oas [post] /admin/inventory-items/{id} @@ -159,6 +160,18 @@ export class AdminPostInventoryItemsInventoryItemReq { @IsNumber() width?: number + @IsString() + @IsOptional() + title?: string + + @IsString() + @IsOptional() + description?: string + + @IsString() + @IsOptional() + thumbnail?: string + @IsBoolean() @IsOptional() requires_shipping?: boolean diff --git a/packages/medusa/src/api/routes/admin/reservations/create-reservation.ts b/packages/medusa/src/api/routes/admin/reservations/create-reservation.ts index e069d55eed..7a47b8d3a8 100644 --- a/packages/medusa/src/api/routes/admin/reservations/create-reservation.ts +++ b/packages/medusa/src/api/routes/admin/reservations/create-reservation.ts @@ -1,6 +1,7 @@ +import { IsNumber, IsObject, IsOptional, IsString } from "class-validator" + import { IInventoryService } from "@medusajs/types" import { isDefined } from "@medusajs/utils" -import { IsNumber, IsObject, IsOptional, IsString } from "class-validator" import { validateUpdateReservationQuantity } from "./utils/validate-reservation-quantity" /** @@ -73,6 +74,8 @@ export default async (req, res) => { const inventoryService: IInventoryService = req.scope.resolve("inventoryService") + const userId: string = req.user.id || req.user.userId + if (isDefined(validatedBody.line_item_id)) { await validateUpdateReservationQuantity( validatedBody.line_item_id, @@ -84,9 +87,10 @@ export default async (req, res) => { ) } - const reservation = await inventoryService.createReservationItem( - validatedBody - ) + const reservation = await inventoryService.createReservationItem({ + ...validatedBody, + created_by: userId, + }) res.status(200).json({ reservation }) } @@ -128,6 +132,10 @@ export class AdminPostReservationsReq { @IsNumber() quantity: number + @IsString() + @IsOptional() + description?: string + @IsObject() @IsOptional() metadata?: Record diff --git a/packages/medusa/src/api/routes/admin/reservations/update-reservation.ts b/packages/medusa/src/api/routes/admin/reservations/update-reservation.ts index 61d7b669a7..e16de26a8f 100644 --- a/packages/medusa/src/api/routes/admin/reservations/update-reservation.ts +++ b/packages/medusa/src/api/routes/admin/reservations/update-reservation.ts @@ -1,8 +1,9 @@ -import { IInventoryService } from "@medusajs/types" -import { isDefined } from "@medusajs/utils" import { IsNumber, IsObject, IsOptional, IsString } from "class-validator" + import { EntityManager } from "typeorm" +import { IInventoryService } from "@medusajs/types" import { LineItemService } from "../../../../services" +import { isDefined } from "@medusajs/utils" import { validateUpdateReservationQuantity } from "./utils/validate-reservation-quantity" /** @@ -120,6 +121,10 @@ export class AdminPostReservationsReservationReq { @IsOptional() location_id?: string + @IsString() + @IsOptional() + description?: string + @IsObject() @IsOptional() metadata?: Record diff --git a/packages/types/src/inventory/common.ts b/packages/types/src/inventory/common.ts index 33e8f9fa66..1a2cf447a5 100644 --- a/packages/types/src/inventory/common.ts +++ b/packages/types/src/inventory/common.ts @@ -21,6 +21,15 @@ import { * mid_code: * description: The Manufacturers Identification code that identifies the manufacturer of the Inventory Item. May be used by Fulfillment Providers to pass customs information to shipping carriers. * type: string + * title: + * description: "Title of the inventory item" + * type: string + * description: + * description: "Description of the inventory item" + * type: string + * thumbnail: + * description: "Thumbnail for the inventory item" + * type: string * material: * description: The material and composition that the Inventory Item is made of, May be used by Fulfillment Providers to pass customs information to shipping carriers. * type: string @@ -68,6 +77,9 @@ export type InventoryItemDTO = { length?: number | null height?: number | null width?: number | null + title?: string | null + description?: string | null + thumbnail?: string | null metadata?: Record | null created_at: string | Date updated_at: string | Date @@ -94,6 +106,12 @@ export type InventoryItemDTO = { * inventory_item_id: * description: "The id of the inventory item the reservation relates to" * type: string + * description: + * description: "Description of the reservation item" + * type: string + * created_by: + * description: "UserId of user who created the reservation item" + * type: string * quantity: * description: "The id of the reservation item" * type: number @@ -120,6 +138,8 @@ export type ReservationItemDTO = { inventory_item_id: string quantity: number line_item_id?: string | null + description?: string | null + created_by?: string | null metadata: Record | null created_at: string | Date updated_at: string | Date @@ -184,6 +204,8 @@ export type FilterableReservationItemProps = { line_item_id?: string | string[] inventory_item_id?: string | string[] location_id?: string | string[] + description?: string + created_by?: string | string[] quantity?: number | NumericalComparisonOperator } @@ -206,6 +228,9 @@ export type CreateInventoryItemInput = { length?: number height?: number width?: number + title?: string + description?: string + thumbnail?: string metadata?: Record | null hs_code?: string requires_shipping?: boolean @@ -216,8 +241,10 @@ export type CreateReservationItemInput = { inventory_item_id: string location_id: string quantity: number - metadata?: Record | null + description?: string + created_by?: string external_id?: string + metadata?: Record | null } export type FilterableInventoryLevelProps = { @@ -244,6 +271,7 @@ export type UpdateInventoryLevelInput = { export type UpdateReservationItemInput = { quantity?: number location_id?: string + description?: string metadata?: Record | null }