Skip to content

Commit

Permalink
chore: merge with latest base
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Jan 11, 2023
2 parents ff45176 + ada3937 commit f3e91e0
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 26 deletions.
5 changes: 4 additions & 1 deletion integration-tests/api/__tests__/admin/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ describe("/admin/product-categories", () => {
is_active: false,
created_at: expect.any(String),
updated_at: expect.any(String),
parent_category_id: productCategoryParent.id,
parent_category: expect.objectContaining({
id: productCategoryParent.id
}),
category_children: []
}),
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { EntityManager } from "typeorm"

import { ProductCategoryService } from "../../../../services"
import { AdminProductCategoriesReqBase } from "../../../../types/product-category"
import { FindParams } from "../../../../types/common"

/**
* @oas [post] /product-categories
* operationId: "PostProductCategories"
* summary: "Create a Product Category"
* description: "Creates a Product Category."
* x-authenticated: true
* parameters:
* - (query) expand {string} (Comma separated) Which fields should be expanded in each product category.
* - (query) fields {string} (Comma separated) Which fields should be retrieved in each product category.
* requestBody:
* content:
* application/json:
Expand All @@ -24,7 +28,8 @@ import { AdminProductCategoriesReqBase } from "../../../../types/product-categor
* const medusa = new Medusa({ baseUrl: MEDUSA_BACKEND_URL, maxRetries: 3 })
* // must be previously logged in or use api token
* medusa.admin.productCategories.create({
* name: 'Jeans'
* name: 'Jeans',
* handle: 'jeans',
* })
* .then(({ productCategory }) => {
* console.log(productCategory.id);
Expand All @@ -36,7 +41,8 @@ import { AdminProductCategoriesReqBase } from "../../../../types/product-categor
* --header 'Authorization: Bearer {api_token}' \
* --header 'Content-Type: application/json' \
* --data-raw '{
* "name": "Jeans"
* "name": "Jeans",
* "handle": "jeans",
* }'
* security:
* - api_token: []
Expand Down Expand Up @@ -82,7 +88,10 @@ export default async (req: Request, res: Response) => {
.create(validatedBody)
})

const productCategory = await productCategoryService.retrieve(created.id)
const productCategory = await productCategoryService.retrieve(
created.id,
req.retrieveConfig,
)

res.status(200).json({ product_category: productCategory })
}
Expand All @@ -106,6 +115,9 @@ export default async (req: Request, res: Response) => {
* is_active:
* type: boolean
* description: A flag to make product category visible/hidden in the store front
* parent_category_id:
* type: string
* description: The ID of the parent product category
*/
// eslint-disable-next-line max-len
export class AdminPostProductCategoriesReq extends AdminProductCategoriesReqBase {
Expand All @@ -117,3 +129,5 @@ export class AdminPostProductCategoriesReq extends AdminProductCategoriesReqBase
@IsNotEmpty()
handle: string
}

export class AdminPostProductCategoriesParams extends FindParams {}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import listProductCategories, {

import createProductCategory, {
AdminPostProductCategoriesReq,
AdminPostProductCategoriesParams,
} from "./create-product-category"

import updateProductCategory, {
Expand All @@ -34,6 +35,11 @@ export default (app) => {

route.post(
"/",
transformQuery(AdminPostProductCategoriesParams, {
defaultFields: defaultProductCategoryFields,
defaultRelations: defaultAdminProductCategoryRelations,
isList: false,
}),
transformBody(AdminPostProductCategoriesReq),
middlewares.wrap(createProductCategory)
)
Expand Down Expand Up @@ -84,4 +90,6 @@ export const defaultProductCategoryFields = [
"handle",
"is_active",
"is_internal",
"created_at",
"updated_at",
]
2 changes: 1 addition & 1 deletion packages/medusa/src/models/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ProductCategory extends SoftDeletableEntity {

// Typeorm also keeps track of the category's parent at all times.
@Column()
parent_category_id: ProductCategory
parent_category_id: string | null

@TreeChildren({ cascade: true })
category_children: ProductCategory[]
Expand Down
5 changes: 3 additions & 2 deletions packages/medusa/src/services/__tests__/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ describe("CartService", () => {
{
id: IdMap.getId("test-session"),
provider_id: "test-provider",
is_selected: true,
is_initiated: true,
},
],
})
Expand Down Expand Up @@ -1463,6 +1463,7 @@ describe("CartService", () => {
IdMap.getId("test-session"),
{
is_selected: true,
is_initiated: true,
}
)
})
Expand Down Expand Up @@ -1559,7 +1560,7 @@ describe("CartService", () => {
shipping_methods: [],
payment_sessions: [
{ provider_id: provider1Id, is_initiated: true },
{ provider_id: provider2Id, is_selected: true },
{ provider_id: provider2Id, is_selected: true, is_initiated: true },
],
region: {
payment_providers: [{ id: provider1Id }, { id: provider2Id }],
Expand Down
51 changes: 40 additions & 11 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1663,11 +1663,37 @@ class CartService extends TransactionBaseService {
)
}

let currentlySelectedSession = cart.payment_sessions.find(
(s) => s.is_selected
)

if (
currentlySelectedSession &&
currentlySelectedSession.provider_id !== providerId
) {
const psRepo = transactionManager.getCustomRepository(
this.paymentSessionRepository_
)

if (currentlySelectedSession.is_initiated) {
await this.paymentProviderService_
.withTransaction(transactionManager)
.deleteSession(currentlySelectedSession)

currentlySelectedSession = psRepo.create(currentlySelectedSession)
}

currentlySelectedSession.is_initiated = false
currentlySelectedSession.is_selected = false
await psRepo.save(currentlySelectedSession)
}

const cartPaymentSessionIds = cart.payment_sessions.map((p) => p.id)
await psRepo.update(
{ id: In(cartPaymentSessionIds) },
{
is_selected: null,
is_initiated: false,
}
)

Expand All @@ -1691,21 +1717,22 @@ class CartService extends TransactionBaseService {
payment_session_id: paymentSession.id,
}

if (paymentSession.is_selected) {
if (paymentSession.is_initiated) {
// update the session remotely
await this.paymentProviderService_
.withTransaction(transactionManager)
.updateSession(paymentSession, sessionInput)
}

if (!paymentSession.is_initiated) {
} else {
// Create the session remotely
paymentSession = await this.paymentProviderService_
.withTransaction(transactionManager)
.createSession(sessionInput)
}

await psRepo.update(paymentSession.id, { is_selected: true })
await psRepo.update(paymentSession.id, {
is_selected: true,
is_initiated: true,
})

await this.eventBus_
.withTransaction(transactionManager)
Expand Down Expand Up @@ -1763,7 +1790,7 @@ class CartService extends TransactionBaseService {

// Helpers that either delete a session locally or remotely. Will be used in multiple places below.
const deleteSessionAppropriately = async (session) => {
if (session.is_selected || session.is_initiated) {
if (session.is_initiated) {
return paymentProviderServiceTx.deleteSession(session)
}

Expand Down Expand Up @@ -1802,7 +1829,7 @@ class CartService extends TransactionBaseService {
if (!providerSet.has(session.provider_id)) {
/**
* if the provider does not belong to the region then delete the session.
* The deletion occurs locally if there is no external data or if it is not selected
* The deletion occurs locally if the session is not initiated
* otherwise the deletion will also occur remotely through the external provider.
*/

Expand All @@ -1813,8 +1840,6 @@ class CartService extends TransactionBaseService {
* if the provider belongs to the region then update or delete the session.
* The update occurs locally if it is not selected
* otherwise the update will also occur remotely through the external provider.
* In case the session is not selected but contains an external provider data, we delete the external provider
* session to be in a clean state.
*/

// We are saving the provider id on which the work below will be done. That way,
Expand All @@ -1823,7 +1848,7 @@ class CartService extends TransactionBaseService {
alreadyConsumedProviderIds.add(session.provider_id)

// Update remotely
if (session.is_selected) {
if (session.is_selected && session.is_initiated) {
const paymentSessionInput = {
...partialSessionInput,
provider_id: session.provider_id,
Expand All @@ -1843,6 +1868,7 @@ class CartService extends TransactionBaseService {
await paymentProviderServiceTx.deleteSession(session)
updatedSession = psRepo.create({
...partialPaymentSessionData,
is_initiated: false,
provider_id: session.provider_id,
})
} else {
Expand Down Expand Up @@ -1872,7 +1898,10 @@ class CartService extends TransactionBaseService {
.withTransaction(transactionManager)
.createSession(paymentSessionInput)

await psRepo.update(paymentSession.id, { is_selected: true })
await psRepo.update(paymentSession.id, {
is_selected: true,
is_initiated: true,
})
return
}

Expand Down
10 changes: 4 additions & 6 deletions packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isDefined, MedusaError } from "medusa-core-utils"
import { EntityManager } from "typeorm"
import { EntityManager, DeepPartial } from "typeorm"
import { TransactionBaseService } from "../interfaces"
import { ProductCategory } from "../models"
import { ProductCategoryRepository } from "../repositories/product-category"
import { FindConfig, Selector, QuerySelector } from "../types/common"
import { buildQuery } from "../utils"
import {
CreateProductCategory,
CreateProductCategoryInput,
UpdateProductCategory,
} from "../types/product-category"

Expand Down Expand Up @@ -109,7 +109,7 @@ class ProductCategoryService extends TransactionBaseService {
* @return created product category
*/
async create(
productCategory: CreateProductCategory
productCategory: CreateProductCategoryInput
): Promise<ProductCategory> {
return await this.atomicPhase_(async (manager) => {
const pcRepo = manager.getCustomRepository(this.productCategoryRepo_)
Expand Down Expand Up @@ -162,7 +162,7 @@ class ProductCategoryService extends TransactionBaseService {
}).catch((err) => void 0)

if (!productCategory) {
return Promise.resolve()
return
}

if (productCategory.category_children.length > 0) {
Expand All @@ -173,8 +173,6 @@ class ProductCategoryService extends TransactionBaseService {
}

await productCategoryRepository.delete(productCategory.id)

return Promise.resolve()
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/types/product-category.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Transform } from "class-transformer"
import { IsNotEmpty, IsOptional, IsString, IsBoolean } from "class-validator"

export type CreateProductCategory = {
export type CreateProductCategoryInput = {
name: string
handle: string
is_internal?: boolean
is_active?: boolean
parent_category_id?: string | null
}

export type UpdateProductCategory = {
Expand All @@ -25,7 +26,6 @@ export class AdminProductCategoriesReqBase {
@IsOptional()
is_active?: boolean

@IsString()
@IsOptional()
@Transform(({ value }) => {
return value === "null" ? null : value
Expand Down

0 comments on commit f3e91e0

Please sign in to comment.