Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProductGroupOption service creation is not creating the GroupOption given #2577

Closed
Caryntjen opened this issue Dec 12, 2023 · 4 comments
Closed
Assignees
Labels
type: bug 🐛 Something isn't working

Comments

@Caryntjen
Copy link

Caryntjen commented Dec 12, 2023

Describe the bug
When using the ProductGroupOption, I can give it an CreateProductOptionGroupInput, this has the property to add options:
CreateGroupOptionInput but these are not added in the db?

To Reproduce
Here is the script:

import {INestApplicationContext} from '@nestjs/common';
import {
    bootstrapWorker,
    ConfigService,
    FacetService,
    FacetValueService, LanguageCode, ProductOptionGroupService, ProductOptionService,
    RequestContext,
    RequestContextService,
    TransactionalConnection,
    User,
} from '@vendure/core';
import path from 'path';
import 'reflect-metadata';
import { config } from '../vendure-config';

if (require.main === module) {
    importDataInitialData().then(
        () => process.exit(0),
        err => {
            console.log(err);
            process.exit(1);
        },
    );
}
export type InputMaybe<T> = T;
export type Scalars = {
    ID: { input: string; output: string; }
    String: { input: string; output: string; }
    Boolean: { input: boolean; output: boolean; }
    Int: { input: number; output: number; }
    Float: { input: number; output: number; }
    DateTime: { input: Date; output: string; }
    JSON: { input: any; output: any; }
    Money: { input: number; output: number; }
    Upload: { input: any; output: any; }
};

export type CreateGroupOptionInput = {
    code: Scalars['String']['input'];
    translations: Array<ProductOptionGroupTranslationInput>;
};
export type ProductOptionGroupTranslationInput = {
    customFields?: InputMaybe<Scalars['JSON']['input']>;
    id?: InputMaybe<Scalars['ID']['input']>;
    languageCode: LanguageCode;
    name?: InputMaybe<Scalars['String']['input']>;
};
export type CreateProductOptionGroupInput = {
    code: Scalars['String']['input'];
    customFields?: InputMaybe<Scalars['JSON']['input']>;
    options: Array<CreateGroupOptionInput>;
    translations: Array<ProductOptionGroupTranslationInput>;
};
async function importDataInitialData() {
    // We use the bootstrapWorker() function instead of bootstrap() because we don't
    // need to start the server, we just need access to the services.
    const {app} = await bootstrapWorker(config);

    // Let's grab a reference to each of the Vendure services we'll need.
    const productGroupOptionService = app.get(ProductOptionGroupService);

    // Most service methods require a RequestContext, so we'll create one here.
    const ctx = await getSuperadminContext(app);
    const productOptionInput: CreateProductOptionGroupInput = {
        code: 'productOptions',
        options: [{
            code: 'option1',
            translations: [
                { languageCode: LanguageCode.en, name: 'option1' }
            ],
        },
            {
                code: 'option2',
                translations: [
                    { languageCode: LanguageCode.en, name: 'option2' }
                ],
            }],
        translations: [
            { languageCode: LanguageCode.en, name: 'productOptions' }
        ],
    };
    const value = await productGroupOptionService.create(ctx, productOptionInput);


    // Close the app
    await app.close();
}

/**
 * Creates a RequestContext configured for the default Channel with the activeUser set
 * as the superadmin user.
 */
export async function getSuperadminContext(app: INestApplicationContext): Promise<RequestContext> {
    const {superadminCredentials} = app.get(ConfigService).authOptions;
    const superAdminUser = await app
        .get(TransactionalConnection)
        .rawConnection.getRepository(User)
        .findOneOrFail({where: {identifier: superadminCredentials.identifier}});
    return app.get(RequestContextService).create({
        apiType: 'admin',
        user: superAdminUser,
    });
}


Expected behavior
CreateGroupOptionInput should be save!

Environment (please complete the following information):

  • @vendure/core version: 2.1.4
  • Nodejs version: 20.10.0
  • Database (mysql/postgres etc): Postgres

Additional context
This is run through a CLI script.

Codgen generates these things:
ProductOptionGroupInput

  • has options CreateGroupOptionInput (2)
  • has translations ProductOptionGroupTranslationInput (1)

CreateGroupOptionInput (2 - Should it not be a ProductOption (instead of GroupOption)?)

  • has translations ProductOptionGroupTranslationInput (1 - should this not be a seperate translation?)

These are used for ProductGroupOptionService.create(ctx, ProductOptionGroupInput)

This is not used:
CreateProductOptionInput

@Caryntjen Caryntjen added the type: bug 🐛 Something isn't working label Dec 12, 2023
@michaelbromley
Copy link
Member

Yes, you are right - the options don't get saved by that method. If you look at the createProductOptionGroup resolver you'll see that you actually have to call productOptionService.create for each option.

@Caryntjen
Copy link
Author

Ok, that was indeed how I resolved it but I thought it could be done in 1 go.

@vjung28
Copy link
Contributor

vjung28 commented Feb 25, 2024

Shouldn't the option parameter in productOptionGroupService.create be at least optional as it does not create the options?

@michaelbromley
Copy link
Member

Yes, I'll improve the type signature of that method to omit the options property.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants