refactor(medusa): Migrate ProductService to TS (#1625)

* refactor: product service

* fix: type errors in consumers

* fix: use Status enum instead of ProductStatus

* fix: remove ProductStatus

* fix: rename ProductStatus

* fix: product model nullable fields

* fix: explicit typecasting in listVariants

* fix: use atomicPhase in public methods

* fix: use transactionManager in protected methods

* fix: remove disable eslint rule comment

* fix: retrieveVariants relations

* fix: retrieveVariants

* fix: FilterableProductProps validation

* fix: nullable thumbnail

* fix: tests by making model column types more explicit

* move upsert method to repo layer

* fix: unit tests

* fix: integration tests

* fix: product tags query + integration test

* fix: rename FindWithRelationsOptions to FindWithoutRelationsOptions

* fix (productRepository): use string[] for relations

* refactor: extract price relation filtering logic into util

* fix: failing unit test

* rename DTO suffix to Input suffix

* fix: missing awaits

* fix: check for images and tags length

* fix: remove unneeded function

* extract types used in product service to types folder

* fix: use text instead of varchar

* remove: reorderOptions from ProductService

* fix: product model

* fix: add private retrieve method

* fix: conflicts

* fix: failing unit test

* fix: integration test

* fix: remove ProductSelector type

* fix: remove validateId

* fix: use spread operator

* fix: repo method typings

* fix: remove comment
This commit is contained in:
Zakaria El Asri
2022-06-20 10:50:46 +01:00
committed by GitHub
parent 9e686a8e47
commit f0be31120f
32 changed files with 808 additions and 650 deletions
+12 -14
View File
@@ -7,18 +7,16 @@ import {
import { FindOperator, In, Raw } from "typeorm"
/**
* Used to build TypeORM queries.
* @param selector The selector
* @param config The config
* @return The QueryBuilderConfig
*/
export function buildQuery<TEntity = unknown>(
selector: Selector<TEntity>,
* Used to build TypeORM queries.
* @param selector The selector
* @param config The config
* @return The QueryBuilderConfig
*/
export function buildQuery<TWhereKeys, TEntity = unknown>(
selector: TWhereKeys,
config: FindConfig<TEntity> = {}
): ExtendedFindConfig<TEntity> {
const build = (
obj: Selector<TEntity>
): Partial<Writable<TEntity>> => {
): ExtendedFindConfig<TEntity, TWhereKeys> {
const build = (obj: Selector<TEntity>): Partial<Writable<TWhereKeys>> => {
return Object.entries(obj).reduce((acc, [key, value]: any) => {
// Undefined values indicate that they have no significance to the query.
// If the query is looking for rows where a column is not set it should use null instead of undefined
@@ -75,10 +73,10 @@ export function buildQuery<TEntity = unknown>(
}
return acc
}, {} as Partial<Writable<TEntity>>)
}, {} as Partial<Writable<TWhereKeys>>)
}
const query: ExtendedFindConfig<TEntity> = {
const query: ExtendedFindConfig<TEntity, TWhereKeys> = {
where: build(selector),
}
@@ -107,4 +105,4 @@ export function buildQuery<TEntity = unknown>(
}
return query
}
}
@@ -0,0 +1,15 @@
/**
*
* @param relations relations from which a relation should be removed
* @param relation relation to be removed
* @returns tuple containing the new relations and a boolean indicating whether the relation was found in the relations array
*/
export const omitRelationIfExists = (
relations: string[],
relation: string
): [string[], boolean] => {
const filteredRelations = relations.filter((rel) => rel !== relation)
const includesRelation = relations.length !== filteredRelations.length
return [relations, includesRelation]
}
+1 -1
View File
@@ -7,7 +7,7 @@ import { MedusaError } from "medusa-core-utils/dist"
* @return resolves to the updated result.
*/
export function setMetadata(
obj: { metadata: Record<string, unknown> },
obj: { metadata: Record<string, unknown> | null },
metadata: Record<string, unknown>
): Record<string, unknown> {
const existing = obj.metadata || {}