fix(stock-location): Stock location address required (#3065)

What:
- Removes the requirement to have an address id while creating a new stock location.
- Add field company to Location Address

FIXES: CORE-1018

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Carlos R. L. Rodrigues
2023-01-23 09:47:04 -03:00
committed by GitHub
parent f65f590a27
commit 2d525237b6
8 changed files with 51 additions and 32 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/stock-location": patch
---
Fix (stock-location): stock location address required

View File

@@ -92,6 +92,10 @@ class StockLocationAddress {
@IsString() @IsString()
address_2?: string address_2?: string
@IsOptional()
@IsString()
company?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
city?: string city?: string

View File

@@ -92,6 +92,10 @@ class StockLocationAddress {
@IsString() @IsString()
address_2?: string address_2?: string
@IsOptional()
@IsString()
company?: string
@IsOptional() @IsOptional()
@IsString() @IsString()
city?: string city?: string

View File

@@ -23,6 +23,10 @@ import { StringComparisonOperator } from "./common"
* type: string * type: string
* description: Stock location address' complement * description: Stock location address' complement
* example: apartment 4432 * example: apartment 4432
* company:
* type: string
* description: Stock location company' name
* example: Medusa
* city: * city:
* type: string * type: string
* description: Stock location address' city * description: Stock location address' city
@@ -64,6 +68,7 @@ export type StockLocationAddressDTO = {
id?: string id?: string
address_1: string address_1: string
address_2?: string | null address_2?: string | null
company?: string | null
country_code: string country_code: string
city?: string | null city?: string | null
phone?: string | null phone?: string | null

View File

@@ -55,6 +55,7 @@ export class setup1665749860179 implements MigrationInterface {
"deleted_at" TIMESTAMP WITH TIME zone, "deleted_at" TIMESTAMP WITH TIME zone,
"address_1" TEXT NOT NULL, "address_1" TEXT NOT NULL,
"address_2" TEXT, "address_2" TEXT,
"company" TEXT,
"city" TEXT, "city" TEXT,
"country_code" TEXT NOT NULL, "country_code" TEXT NOT NULL,
"phone" TEXT, "phone" TEXT,
@@ -73,12 +74,12 @@ export class setup1665749860179 implements MigrationInterface {
"updated_at" TIMESTAMP WITH time zone NOT NULL DEFAULT Now(), "updated_at" TIMESTAMP WITH time zone NOT NULL DEFAULT Now(),
"deleted_at" TIMESTAMP WITH time zone, "deleted_at" TIMESTAMP WITH time zone,
"name" TEXT NOT NULL, "name" TEXT NOT NULL,
"address_id" TEXT NOT NULL, "address_id" TEXT,
"metadata" JSONB, "metadata" JSONB,
CONSTRAINT "PK_adf770067d0df1421f525fa25cc" PRIMARY KEY ("id") CONSTRAINT "PK_adf770067d0df1421f525fa25cc" PRIMARY KEY ("id")
); );
CREATE INDEX "IDX_stock_location_address_id" ON "stock_location" ("address_id"); CREATE INDEX "IDX_stock_location_address_id" ON "stock_location" ("address_id") WHERE deleted_at IS NOT NULL;
`) `)
} }

View File

@@ -9,6 +9,9 @@ export class StockLocationAddress extends SoftDeletableEntity {
@Column({ type: "text", nullable: true }) @Column({ type: "text", nullable: true })
address_2: string | null address_2: string | null
@Column({ type: "text", nullable: true })
company: string | null
@Column({ type: "text", nullable: true }) @Column({ type: "text", nullable: true })
city: string | null city: string | null

View File

@@ -16,8 +16,8 @@ export class StockLocation extends SoftDeletableEntity {
name: string name: string
@Index() @Index()
@Column({ type: "text" }) @Column({ type: "text", nullable: true })
address_id: string address_id: string | null
@ManyToOne(() => StockLocationAddress) @ManyToOne(() => StockLocationAddress)
@JoinColumn({ name: "address_id" }) @JoinColumn({ name: "address_id" })

View File

@@ -62,9 +62,9 @@ export default class StockLocationService extends TransactionBaseService {
/** /**
* Lists all stock locations that match the given selector. * Lists all stock locations that match the given selector.
* @param {FilterableStockLocationProps} [selector={}] - Properties to filter by. * @param selector - Properties to filter by.
* @param {FindConfig} [config={ relations: [], skip: 0, take: 10 }] - Additional configuration for the query. * @param config - Additional configuration for the query.
* @return {Promise<StockLocation[]>} A list of stock locations. * @return A list of stock locations.
*/ */
async list( async list(
selector: FilterableStockLocationProps = {}, selector: FilterableStockLocationProps = {},
@@ -79,9 +79,9 @@ export default class StockLocationService extends TransactionBaseService {
/** /**
* Lists all stock locations that match the given selector and returns the count of matching stock locations. * Lists all stock locations that match the given selector and returns the count of matching stock locations.
* @param {FilterableStockLocationProps} [selector={}] - Properties to filter by. * @param selector - Properties to filter by.
* @param {FindConfig} [config={ relations: [], skip: 0, take: 10 }] - Additional configuration for the query. * @param config - Additional configuration for the query.
* @return {Promise<[StockLocation[], number]>} A list of stock locations and the count of matching stock locations. * @return A list of stock locations and the count of matching stock locations.
*/ */
async listAndCount( async listAndCount(
selector: FilterableStockLocationProps = {}, selector: FilterableStockLocationProps = {},
@@ -95,12 +95,11 @@ export default class StockLocationService extends TransactionBaseService {
} }
/** /**
* Retrieves a stock location by its ID. * Retrieves a Stock Location by its ID.
* @param {string} stockLocationId - The ID of the stock location. * @param stockLocationId - The ID of the stock location.
* @param {FindConfig} [config={}] - Additional configuration for the query. * @param config - Additional configuration for the query.
* @return {Promise<StockLocation>} The stock location. * @return The stock location.
* @throws {MedusaError} If the stock location ID is not defined. * @throws If the stock location ID is not definedor the stock location with the given ID was not found.
* @throws {MedusaError} If the stock location with the given ID was not found.
*/ */
async retrieve( async retrieve(
stockLocationId: string, stockLocationId: string,
@@ -131,8 +130,8 @@ export default class StockLocationService extends TransactionBaseService {
/** /**
* Creates a new stock location. * Creates a new stock location.
* @param {CreateStockLocationInput} data - The input data for creating a stock location. * @param data - The input data for creating a Stock Location.
* @returns {Promise<StockLocation>} - The created stock location. * @returns The created stock location.
*/ */
async create(data: CreateStockLocationInput): Promise<StockLocation> { async create(data: CreateStockLocationInput): Promise<StockLocation> {
return await this.atomicPhase_(async (manager) => { return await this.atomicPhase_(async (manager) => {
@@ -145,10 +144,7 @@ export default class StockLocationService extends TransactionBaseService {
if (isDefined(data.address) || isDefined(data.address_id)) { if (isDefined(data.address) || isDefined(data.address_id)) {
if (typeof data.address === "string" || data.address_id) { if (typeof data.address === "string" || data.address_id) {
const addrId = (data.address ?? data.address_id) as string const addrId = (data.address ?? data.address_id) as string
const address = await this.retrieve(addrId, { loc.address_id = addrId
select: ["id"],
})
loc.address_id = address.id
} else { } else {
const locAddressRepo = manager.getRepository(StockLocationAddress) const locAddressRepo = manager.getRepository(StockLocationAddress)
const locAddress = locAddressRepo.create(data.address!) const locAddress = locAddressRepo.create(data.address!)
@@ -176,9 +172,9 @@ export default class StockLocationService extends TransactionBaseService {
/** /**
* Updates an existing stock location. * Updates an existing stock location.
* @param {string} stockLocationId - The ID of the stock location to update. * @param stockLocationId - The ID of the stock location to update.
* @param {UpdateStockLocationInput} updateData - The update data for the stock location. * @param updateData - The update data for the stock location.
* @returns {Promise<StockLocation>} - The updated stock location. * @returns The updated stock location.
*/ */
async update( async update(
@@ -223,10 +219,10 @@ export default class StockLocationService extends TransactionBaseService {
} }
/** /**
* Updates an address for a stock location. * Updates an address for a Stock Location.
* @param {string} addressId - The ID of the address to update. * @param addressId - The ID of the address to update.
* @param {StockLocationAddressInput} address - The update data for the address. * @param address - The update data for the address.
* @returns {Promise<StockLocationAddress>} - The updated stock location address. * @returns The updated stock location address.
*/ */
protected async updateAddress( protected async updateAddress(
@@ -265,9 +261,9 @@ export default class StockLocationService extends TransactionBaseService {
} }
/** /**
* Deletes a stock location. * Deletes a Stock Location.
* @param {string} id - The ID of the stock location to delete. * @param id - The ID of the stock location to delete.
* @returns {Promise<void>} - An empty promise. * @returns An empty promise.
*/ */
async delete(id: string): Promise<void> { async delete(id: string): Promise<void> {
return await this.atomicPhase_(async (manager) => { return await this.atomicPhase_(async (manager) => {