Skip to content

Origin Backend - @energyweb/origin-backend

Source code on GitHub

Overview

The Origin Backend is a NestJS application that provides services for user and organization authorization and management. The Backend application can be used in conjunction with one, several or all of the Origin SDKs to provide integrated user management and authorization.

The below gives an overview the of the package architecture, however the NestJS documentation provides further detail into the fundamentals of NestJS Architecture that may help to understand the elements of this application:

Origin Backend Architecture

The Origin Backend package is broken down into seven NestJS modules:

Each module contains code relevant for a specific feature. In general, each NestJS module has:

  • A controller that manages requests and responses to the client
  • A .entity file that maps an entity to the database repository
  • A .service file that provides methods to fetch and transform data
  • Data Transfer Object (DTO) file(s) that provide Data Transfer Objects, which are representations of the data that are exposed to the endpoint consumer
  • A module class that is used by NestJS to structure the application

Persistence

The Origin Backend uses PostgreSQL for persistence with TypeORM as a database integration library. The application creates a repository for each entity. Entities are defined in the .entity.ts file in each module, and are marked with the @Entity decorator. (You can read more about entities in the TypeORM documentation here).

@Entity('email_confirmation')
export class EmailConfirmation extends ExtendedBaseEntity implements IEmailConfirmation {
    @PrimaryGeneratedColumn()
    id: number;

    @OneToOne(() => User)
    @JoinColumn()
    user: User;

    @Column()
    @IsBoolean()
    confirmed: boolean;

    @Column()
    @IsString()
    token: string;

    @Column()
    @IsInt()
    @Min(0)
    expiryTimestamp: number;
}

source

Repositories are injected into services or command handlers so they are available to use in methods:

@Injectable()
export class EmailConfirmationService {
    constructor(
        @InjectRepository(EmailConfirmation)
        private readonly repository: Repository<EmailConfirmation>,
        private readonly eventBus: EventBus
    ) {}

source

Modules

admin

Source code on GitHub

The admin module's controller uses the user service to fetch and update user information.

configuration

Source code on GitHub

The configuration module fetches and updates the platform's configuration values. These values are used to populate the platform's market options.

email-confirmation

Source code on GitHub

The email-confirmation module provides methods to manage the email confirmation process. When users register to join the platform, they must confirm their email. Confirmations are persisted in the Email Confirmation repository.

file

Source code on GitHub

The email-confirmation module provides methods to fetch, update and store files. Files are uploaded to the system when registering an organization, a device or requesting certification. Files are persisted in the File repository.

invitation

Source code on GitHub

The invitation module provides methods to manage invitations for users to join organizations. Invitations are persisted in the Invitation repository.

organization

Source code on GitHub

The invitation module provides methods to manage fetch, create and update Organization data, including Organization Blockchain Account Addresses. Organizations are persisted in the Organization Repository.

user

Source code on GitHub

The user module provides methods to manage fetch, create and update users. Users are persisted in the User Repository.

Origin Backend-Core Package - @energyweb/origin-backend-core

Source code on GitHub

The Origin Backend Core package provides the interfaces, types and enums for Origin Backend.