organizations.service.ts 1.22 KB
Newer Older
1
import { Injectable, Logger } from '@nestjs/common';
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
2 3 4 5
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { Organization } from './organization.entity';
6
import { ConnectorsService } from 'src/connectors/connectors.service';
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
7 8 9

@Injectable()
export class OrganizationsService {
10 11
  logger: Logger;

Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
12 13 14
  constructor(
    @InjectRepository(Organization)
    private organizationsRepository: Repository<Organization>,
15 16 17 18
    private connectorsService: ConnectorsService,
  ) {
    this.logger = new Logger(Organization.name);
  }
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
19

20
  async findAll() {
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
21 22 23
    return this.organizationsRepository.find();
  }

24 25 26 27 28 29 30
  async findByIdentifier(uuid: string) {
    const results = await this.organizationsRepository.find({ take: 1 });
    return results[0];
  }

  async createFromName(name: string) {
    this.logger.log(`Creating organization with name ${name}`);
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
31 32 33
    const organization = new Organization();
    organization.name = name;
    organization.sharedSecret = Organization.randomSecret();
34 35 36 37
    await this.organizationsRepository.save(organization);
    await this.connectorsService.registerOrganization(organization);
    this.logger.log(`Created organization (id: ${organization.id})`);
    return organization;
Hidde-Jan Jongsma's avatar
Hidde-Jan Jongsma committed
38 39
  }
}