Skip to content

Commit

Permalink
docs: fix usage of non-existing repository in digital products recipe (
Browse files Browse the repository at this point in the history
…medusajs#5280)

* docs: fix usage of non-existing repository in digital products recipe

* fix content linting
  • Loading branch information
shahednasser authored Oct 4, 2023
1 parent 130cbc1 commit 6d9c0eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
28 changes: 14 additions & 14 deletions www/apps/docs/content/plugins/cms/contentful.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,28 +310,28 @@ Create the file `src/loaders/contentful-migrations/product-collection.ts` with t

```ts title=src/loaders/contentful-migrations/product-collection.ts
import Migration, {
MigrationContext
} from "contentful-migration";
MigrationContext,
} from "contentful-migration"

export function productCollectionMigration (
export function productCollectionMigration(
migration: Migration,
context?: MigrationContext
) {
const collection = migration
.createContentType("collection")
.name("Product Collection")
.displayField("title");
.displayField("title")

collection
.createField("title")
.name("Title")
.type("Symbol")
.required(true);
.required(true)
collection
.createField("medusaId")
.name("Medusa ID")
.type("Symbol");
};
.type("Symbol")
}
```
</details>

Expand All @@ -344,28 +344,28 @@ Create the file `src/loaders/contentful-migrations/product-type.ts` with the fol

```ts title=src/loaders/contentful-migrations/product-type.ts
import Migration, {
MigrationContext
} from "contentful-migration";
MigrationContext,
} from "contentful-migration"

export function productTypeMigration (
export function productTypeMigration(
migration: Migration,
context?: MigrationContext
) {
const collection = migration
.createContentType("productType")
.name("Product Type")
.displayField("title");
.displayField("title")

collection
.createField("title")
.name("Title")
.type("Symbol")
.required(true);
.required(true)
collection
.createField("medusaId")
.name("Medusa ID")
.type("Symbol");
};
.type("Symbol")
}
```
</details>

Expand Down
52 changes: 21 additions & 31 deletions www/apps/docs/content/recipes/digital-products.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -284,26 +284,18 @@ import {
TransactionBaseService,
buildQuery,
} from "@medusajs/medusa"
import {
ProductMediaRepository,
} from "../repositories/product-media"
import { ProductMedia } from "../models/product-media"
import { MedusaError } from "@medusajs/utils"

type InjectedDependencies = {
productMediaRepository: typeof ProductMediaRepository
productVariantService: ProductVariantService
}

class ProductMediaService extends TransactionBaseService {
protected productMediaRepository_:
typeof ProductMediaRepository
protected productVariantService_: ProductVariantService

constructor(container: InjectedDependencies) {
super(container)
this.productMediaRepository_ =
container.productMediaRepository
this.productVariantService_ =
container.productVariantService
}
Expand All @@ -328,12 +320,12 @@ class ProductMediaService extends TransactionBaseService {
relations: [],
}
): Promise<[ProductMedia[], number]> {
const productMediaRepo = this.activeManager_.withRepository(
this.productMediaRepository_
const productMediaRepo = this.activeManager_.getRepository(
ProductMedia
)

const [
relations,
relations,
isVariantsEnabled,
] = this.checkVariantInRelations(
config.relations || []
Expand All @@ -354,8 +346,7 @@ class ProductMediaService extends TransactionBaseService {
async (media, index) => {
productMedias[index].variant =
await this.retrieveVariantByMedia(media)
})
)
}))
}

return [productMedias, count]
Expand All @@ -370,9 +361,8 @@ class ProductMediaService extends TransactionBaseService {
}
): Promise<ProductMedia[]> {
const [productMedias] = await this.listAndCount(
selector,
config
)
selector, config
)

return productMedias
}
Expand All @@ -381,10 +371,9 @@ class ProductMediaService extends TransactionBaseService {
id: string,
config?: FindConfig<ProductMedia>
): Promise<ProductMedia> {
const productMediaRepo =
this.activeManager_.withRepository(
this.productMediaRepository_
)
const productMediaRepo = this.activeManager_.getRepository(
ProductMedia
)

const query = buildQuery({
id,
Expand All @@ -400,30 +389,31 @@ class ProductMediaService extends TransactionBaseService {
}

if (config.relations.includes("variant")) {
productMedia.variant =
await this.retrieveVariantByMedia(productMedia)
productMedia.variant = await this.retrieveVariantByMedia(
productMedia
)
}

return productMedia
}

async retrieveVariantByMedia(productMedia: ProductMedia) {
return await this.productVariantService_.retrieve(
productMedia.variant_id, {
productMedia.variant_id,
{
relations: ["product"],
}
)
}

async create(
data: Pick<
ProductMedia,
"name" | "file_key" | "variant_id" | "type"
ProductMedia, "name" | "file_key" | "variant_id" | "type"
>
): Promise<ProductMedia> {
return this.atomicPhase_(async (manager) => {
const productMediaRepo = manager.withRepository(
this.productMediaRepository_
const productMediaRepo = manager.getRepository(
ProductMedia
)
const productMedia = productMediaRepo.create(data)
const result = await productMediaRepo.save(productMedia)
Expand All @@ -437,8 +427,8 @@ class ProductMediaService extends TransactionBaseService {
data: Omit<Partial<ProductMedia>, "id">
): Promise<ProductMedia> {
return await this.atomicPhase_(async (manager) => {
const productMediaRepo = manager.withRepository(
this.productMediaRepository_
const productMediaRepo = manager.getRepository(
ProductMedia
)
const productMedia = await this.retrieve(id)

Expand All @@ -450,8 +440,8 @@ class ProductMediaService extends TransactionBaseService {

async delete(id: string): Promise<void> {
return await this.atomicPhase_(async (manager) => {
const productMediaRepo = manager.withRepository(
this.productMediaRepository_
const productMediaRepo = manager.getRepository(
ProductMedia
)
const productMedia = await this.retrieve(id)

Expand Down

0 comments on commit 6d9c0eb

Please sign in to comment.