#I have a problem in logically how to properly seperate these modules?

36 messages · Page 1 of 1 (latest)

limpid spire
#

I have a third party api called Exact.
I fetch products via this API and make authentication and so on.

Now I also have my Models Product, ProductGroup, VatCode, and these are all in seperate modules.

Now what i'm doing is trying to use Exact to save products, productgroups and vatcodes. Because the Prepared data will come from Exact for those models. I will update them with Cron job.

While doing so i'm curious, do I need to fetch products from exactService, handle the data in exactService, and just give call the productService.insertProduct() to add or how to manage these seperations logical properly?

#

  async saveExactPricelists(id?: string): Promise<void> {
    let exactPricelists = await this.exactService.getPricelists(id ?? '');

    if (!exactPricelists && exactPricelists?.length === 0) return;

    await bluebird.each(
      exactPricelists,
      async (exactPricelist, exactPricelistIndex) => {
        let code = exactPricelist?.Code?.toString()?.trim();
        let exactId = exactPricelist?.ID?.toString()?.trim();
        let name = exactPricelist?.Description?.toString()?.trim();
        let currency: Currency =
          exactPricelist?.Currency?.toString()?.trim() ?? Currency.EUR;

        await this.prisma.pricelist.upsert({
          where: {
            code,
            exactId,
          },
          create: {
            code,
            exactId,
            name,
            currency,
          },

          update: {},
        });
      }
    );
  }
#

this is an example, pricelistService.ts

rigid jacinth
#

these are all in seperate modules.
I'd ask if this is actually necessary. How complex are the tasks/ services up till now to manage products? In general, you should think of modules as doing stuff and not as model objects. "Doing stuff" in a module should be containing a logical set of processes for a particular feature, like "ProductManagement". If your sub-features are either reusable or complex, then you might break them out to sub-modules.

limpid spire
#

sub modules mean putting them in 1 module?

rigid jacinth
#

If there is a need to break the services out because of the complexity in them, yes. But, for now, I'd consider this just one module. Refactoring later will be easy. No worries. In other words, don't over-complicate by putting services that are working towards the same feature in different modules. Just create one.

To answer your question more directly, the task of gathering the product data from the API sounds like a service to me. So, maybe you could have an ExactAPIRequestService in your ProductManagement module.

limpid spire
#

hey @rigid jacinth

#

you are basically saying that

#

i can delete product-price module

#

and put it into product

#

right

#

just keeping the product-price.service.ts

#

for an example of merging sub-modules

rigid jacinth
#

Yes, in your product module ( I still don't like that name), you would have folders like controllers, services, guards, pipes, types, etc. One of your services is in the /services folder would be price.service.ts. You don't need the word "product" in the name, because it is in that module, right?

Now imagine, your "price" (I'd actually call it "pricing"), service gets large, and you break the functionality out to other service classes. At that point, you may also want to break them and any matching components (like controllers or other providers) out to their own module. For only two classes, maybe not. But for multiple classes, more than likely. See how that works?

limpid spire
#

i understand thank you

#

i hhave one other question related

#

i have Company Model, Product Model, Pricelist Model, Product Price model

#

each company has 1 pricelist, and those pricelists inserts product + product price

#

so i'm calling pricelistService in companyService when a new company is created

#

but to be able to call productService in pricelistService, do I need to import full ProductModule there?

rigid jacinth
#

Is this going to be multi-tenancy? Like, each company is a tenant of your application?

limpid spire
#

you can think each company is a main level user

#

and prices products are decided by their pricelist id

#

not sure the meaning of tenant

#

company logs in and see their products and prices specifically assigned

#

my question is if i try to take productservice as provider to the pricelist module

#

it wants me to import all providers used in productmodule

#

which is huge

#

so i imported the module itself

#

but not sure if this is correct way to do it

rigid jacinth
#

Tenant means, each user will need to be part of a company, and each company will be the actual customer of your application.

#

This is hugely important to know for the design of your application.

limpid spire
#

very correct

#

CompanyUser table then attached to each companyId, userId scalar relation