Skip to content

SharedBrandToOrganizationGroupsModule

The SharedBrandToOrganizationGroupsModule provides a comprehensive solution for managing and sharing brands among organization groups. This module simplifies brand access control and enhances collaboration, allowing organizations to efficiently allocate resources and facilitate authorized access to relevant brands for users across different organizations within a group.

Entity Inheritance

NestKit provide base classes that can be inherited and customized with your needs. For entities we use TypeORM TableInheritance.

Key Components

The module provides a set of reusable components that can be easily integrated into a NestJS project:

SharedBrandToOrganizationGroup Entity

The SharedBrandToOrganizationGroup is an entity that represents the relationship between brands and organization groups, enabling controlled access and collaboration among users from different organizations.

SharedBrandToOrganizationGroupsService

The SharedBrandToOrganizationGroupsService is a service layer component that manages the sharing of brands with organization groups. It provides essential functionalities, such as creating, updating, and removing brand sharing relationships, to ensure that users from different organizations have appropriate access to shared brands based on their permissions within a group.

SharedBrandToOrganizationGroupsResolver

The SharedBrandToOrganizationGroupsResolver is a GraphQL resolver that handles requests related to sharing brands with organization groups. It exposes a set of GraphQL queries and mutations, enabling clients to interact with the SharedBrandToOrganizationGroupsService.

How to install SharedBrandToOrganizationGroupsModule to my app

To create a SharedBrandToOrganizationGroupsModule in NestJS, you can follow these steps:

  1. Create a shared-brands-to-organization-groups folder in your project's src/modules directory.

  2. Install the required dependencies for SharedBrandToOrganizationGroupsModule:

  3. Inside the shared-brands-to-organization-groups folder, create a shared-brands-to-organization-group.entity.ts file and extends SharedBrandToOrganizationGroup from NestKit with the following code:

    ts
    // src/modules/shared-brands-to-organization-groups/shared-brands-to-organization-group.entity.ts
    
    import { Field, ObjectType } from '@nestjs/graphql';
    import { ChildEntity } from 'typeorm';
    import { Brand } from '../brands/brand.entity';
    import { OrganizationGroup } from '../organization-groups/organization-group.entity';
    import { User } from '@/modules/users/user.entity';
    import { BaseSharedBrandToOrganizationGroup } from '@deeepvision/nest-kit/dist/modules/shared-brands-to-organization-groups';
    import { MaybeNull } from '@deeepvision/nest-kit';
    
    @ObjectType()
    @ChildEntity()
    export class SharedBrandToOrganizationGroup extends BaseSharedBrandToOrganizationGroup {
      @Field(() => Brand)
      brand!: Promise<Brand>;
    
      @Field(() => OrganizationGroup)
      organizationGroup!: Promise<OrganizationGroup>;
    
      @Field(() => User)
      sharedBy!: Promise<MaybeNull<User>>;
    }
  4. Inside the shared-brands-to-organization-groups folder, create a shared-brands-to-organization-groups.service.ts file and extends SharedBrandToOrganizationGroupsService from NestKit with the following code:

    ts
    // src/modules/shared-brands-to-organization-groups/shared-brands-to-organization-groups.service.ts
    
    import { BaseSharedBrandsToOrganizationGroupsService } from '@deeepvision/nest-kit/dist/modules/shared-brands-to-organization-groups';
    import { SharedBrandToOrganizationGroup } from './shared-brand-to-organization-group.entity';
    
    export class SharedBrandsToOrganizationGroupsService extends BaseSharedBrandsToOrganizationGroupsService<
    SharedBrandToOrganizationGroup
    > {}
  5. Inside the shared-brands-to-organization-groups folder, create a shared-brands-to-organization-groups.resolver.ts file and extends BaseSharedBrandsToOrganizationGroupsResolver with the following code:

    ts
    // src/modules/shared-brands-to-organization-groups/shared-brands-to-organization-groups.resolver.ts
    
    import { BaseSharedBrandsToOrganizationGroupsResolver } from '@deeepvision/nest-kit/dist/modules/shared-brands-to-organization-groups';
    import { Resolver } from '@nestjs/graphql';
    import { SharedBrandToOrganizationGroup } from './shared-brand-to-organization-group.entity';
    
    @Resolver(() => SharedBrandToOrganizationGroup)
    export class SharedBrandsToOrganizationGroupsResolver extends BaseSharedBrandsToOrganizationGroupsResolver(
      SharedBrandToOrganizationGroup,
    ) {}
  6. Inside the shared-brands-to-organization-groups folder, create a shared-brands-to-organization-groups.module.ts file with the following code:

    ts
    // src/modules/shared-brands-to-organization-groups/shared-brands-to-organization-groups.module.ts
    
    import { Module } from '@nestjs/common';
    import { TypeOrmModule } from '@nestjs/typeorm';
    import { SharedBrandToOrganizationGroup } from './shared-brand-to-organization-group.entity';
    import { SharedBrandsToOrganizationGroupsResolver } from './shared-brand-to-organization-groups.resolver';
    import { SharedBrandsToOrganizationGroupsService } from './shared-brand-to-organization-groups.service';
    import { SHARED_BRANDS_TO_ORGANIZATION_GROUPS_SERVICE_TOKEN } from '@deeepvision/nest-kit/dist/modules/shared-brands-to-organization-groups';
    
    @Module({
      imports: [
        TypeOrmModule.forFeature([SharedBrandToOrganizationGroup]),
      ],
      providers: [
        SharedBrandsToOrganizationGroupsService,
        {
          provide: SHARED_BRANDS_TO_ORGANIZATION_GROUPS_SERVICE_TOKEN,
          useExisting: SharedBrandsToOrganizationGroupsService,
        },
        SharedBrandsToOrganizationGroupsResolver,
      ],
      exports: [
        SharedBrandsToOrganizationGroupsService,
        {
          provide: SHARED_BRANDS_TO_ORGANIZATION_GROUPS_SERVICE_TOKEN,
          useExisting: SharedBrandsToOrganizationGroupsService,
        },
      ],
    })
    export class SharedBrandsToOrganizationGroupsModule {}
  7. Finally, import the SharedBrandsToOrganizationGroupsModule into your main AppModule or another module:

ts
// src/modules/app.module.ts

import { Module } from '@nestjs/common';
import { SharedBrandsToOrganizationGroupsModule } from './modules/shared-brands-to-organization-groups/shared-brands-to-organization-groups.module.ts';

@Module({
  imports: [SharedBrandsToOrganizationGroupsModule],
})
export class AppModule {}

In this code, we are importing the SharedBrandsToOrganizationGroupsModule and adding it to the imports array of our AppModule.

How to share brand with organization group

Here's an example of how to call SharedBrandsToOrganizationGroupsService.create with an object literal based on CreateSharedBrandToOrganizationGroupOptions:

ts
const sbToOrgGroup = await this.sharedBrandsToOrganizationGroupsService.create({
  brandId: 'hcb:xxxxxxxxxxx',
  organizationGroupId: 'hcgrp:xxxxxxxxxxx',
  accessLevels: ['brand:view'],
  legalNotice: { // RichText
    tiptap: {
      type: 'doc',
      content: [
        {
          type: 'paragraph',
          content: [
            {
              type: 'text',
              text: 'This brand is shared with your organization for viewing and editing purposes only. Any unauthorized use is prohibited.'
            }
          ]
        }
      ]
    }
  }
});

In this example, a brand with ID hcb:xxxxxxxxxxx is being shared with an organization group with ID hcgrp:xxxxxxxxxxx. The provided access level brand:view, allowing the organization group to view the shared brand. A legal notice is also included as a part of the sharing process.

Share brand with organization group with GraphQL

Here's an example GraphQL mutation to share brand with organization group

How to get brand sharing by ID

Use the getOne() method from your SharedBrandsToOrganizationGroupsService to get a sharing by id in other parts of your application.

ts
const sbToOrgGroup = await this.sharedBrandsToOrganizationGroupsService.getOne('sbgrp:xxxxxxxxxxx');

Use the getOneOrFail() method to retrieve a sharing by id or throw an error if not found.

ts
const sbToOrgGroup = await this.sharedBrandsToOrganizationGroupsService.getOneOrFail('sbgrp:xxxxxxxxxxx', ctx);

const organizationGroup = await sbToOrgGroup.organizationGroup;
const brand = await sbToOrgGroup.brand;

console.log(`Brand "${brand.id}" was shared to  organization group with id "${organizationGroup.id}" `)

Fetch brand to org group sharing by id with GraphQL

Here's an example GraphQL query to fetch sharing to org group by id.

How to get sharings list

Use the getMany() method in other parts of your application to retrieve a list of brands sharings to organization groups.

ts
const [sharedBrandsToOrganizationGroups, meta] = await this.sharedBrandsToOrganizationGroupsService.getMany({
  filter: {
    ...
  },
  orderBy: SharedBrandsToOrganizationGroupsOrderBy.createdAt_ASC,
});

See SharedBrandsToOrganizationGroupsOrderBy and SharedBrandsToOrganizationGroupsFilter for more information on how to filter and order user roles.

Fetch sharings list with GraphQL

Here's an example GraphQL query to fetch sharings.

How to delete sharing

Call the delete method of the sharedBrandsToOrganizationGroupsService and pass in the id of the sharing you want to delete:

ts
await this.sharedBrandsToOrganizationGroupsService.delete('sbgrp:xxxxxxxxxxx');

In this example, we are deleting a sharing with an id of sbgrp:xxxxxxxxxxx.

Delete brand to organization group sharing with GraphQL

Here's an example GraphQL mutation to delete brand to organization group sharing.

Created by DeepVision Software.