Skip to content

SharedBrandsToOrganizationsModule

The SharedBrandsToOrganizationsModule enables users to share brands with other organizations. This API facilitates seamless collaboration and resource sharing between organizations, ensuring that users from different organizations have access to the brands they are authorized to use. By utilizing this API, organizations can effectively manage user access and permissions for shared brands, streamlining brand-related operations and promoting cross-organizational cooperation.

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:

SharedBrandToOrganization Entity

The SharedBrandToOrganization entity represents the relationship between a shared brand and an organization. This entity stores essential information such as the brand ID, organization ID, access levels, and any additional legal notices. By managing these associations, the SharedBrandToOrganization entity enables efficient sharing of brands across organizations while maintaining proper access control and permissions for users in each organization.

SharedBrandToOrganizationsService

The SharedBrandsToOrganizationsService is responsible for managing the sharing of brands across different organizations. This service provides essential methods for creating, updating, and deleting associations between brands and organizations.

SharedBrandToOrganizationsResolver

The SharedBrandsToOrganizationsResolver handles GraphQL queries and mutations related to sharing brands with organizations.

How to install SharedBrandToOrganizationModule to my app

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

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

  2. Install the required dependencies for SharedBrandToOrganizationModule:

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

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

    ts
    // src/modules/shared-brands-to-organizations/shared-brands-to-organizations.service.ts
    
    import { BaseSharedBrandsToOrganizationsService } from '@deeepvision/nest-kit/dist/modules/shared-brands-to-organizations';
    import { SharedBrandToOrganization } from './shared-brand-to-organization.entity';
    
    export class SharedBrandsToOrganizationsService extends BaseSharedBrandsToOrganizationsService<SharedBrandToOrganization> {}
  5. Inside the shared-brands-to-organizations folder, create a shared-brands-to-organizations.resolver.ts file and extends BaseSharedBrandsToOrganizationsResolver with the following code:

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

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

    ts
    // src/modules/app.module.ts
    
    import { Module } from '@nestjs/common';
    import { SharedBrandsToOrganizationsModule } from './modules/shared-brands-to-organizations/shared-brands-to-organizations.module.ts';
    
    @Module({
      imports: [SharedBrandsToOrganizationsModule],
    })
    export class AppModule {}

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

How to share brand with organization

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

ts
const sbToOrg = await this.sharedBrandsToOrganizationsService.create({
  brandId: 'hcb:xxxxxxxxxxx',
  organizationId: 'hcorg: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 with ID hcorg:xxxxxxxxxxx. The provided access level brand:view, allowing the organization to view the shared brand. A legal notice is also included as a part of the sharing process.

Share brand with organization with GraphQL

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

How to get brand sharing by ID

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

ts
const sbToOrg = await this.sharedBrandsToOrganizationsService.getOne('sborg:xxxxxxxxxxx');

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

ts
const sbToOrg = await this.sharedBrandsToOrganizationsService.getOneOrFail('sborg:xxxxxxxxxxx', ctx);

const organization = await sbToOrg.organization;
const brand = await sbToOrg.brand;

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

Fetch brand to organization sharing by id with GraphQL

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

How to get brand to organization sharings list

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

ts
const [sharedBrandsToOrganizations, meta] = await this.sharedBrandsToOrganizationsService.getMany({
  filter: {
    ...
  },
  orderBy: SharedBrandsToOrganizationsOrderBy.createdAt_ASC,
});

See SharedBrandsToOrganizationsOrderBy and SharedBrandsToOrganizationsFilter for more information on how to filter and order user roles.

Fetch brand to organization sharings list with GraphQL

Here's an example GraphQL query to fetch sharings.

How to delete brand to organization sharing

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

ts
await this.sharedBrandsToOrganizationsService.delete('sborg:xxxxxxxxxxx');

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

Delete brand to organization sharing with GraphQL

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

Created by DeepVision Software.