Commit 65dddb9c authored by FORESTIER Fabien's avatar FORESTIER Fabien
Browse files

Generate uuid in service instead of in entity to avoid problems when updating

parent ef0aea86
Pipeline #2373 passed with stages
in 4 minutes and 30 seconds
{
"name": "service-organizations",
"version": "2.0.1",
"version": "2.0.2",
"description": "description",
"author": "",
"license": "MIT",
......@@ -81,4 +81,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
\ No newline at end of file
......@@ -32,14 +32,6 @@ export class LinkEntity {
@JoinColumn({ name: 'organizationId' })
@ApiModelProperty()
organization: Organization;
@BeforeInsert()
private beforeInsert() {
if (!this.id) {
this.id = uuidv4();
}
}
}
export interface LinksRO {
......
......@@ -3,6 +3,7 @@ import { LinkEntity, LinksRO } from './link.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Link } from './link.dto';
import { LinkRepository } from './link.repository';
import * as uuidv4 from 'uuid/v4';
@Injectable()
export class LinksService {
......@@ -81,7 +82,8 @@ export class LinksService {
this.logger.log('Entering function', `${LinksService.name} - ${this.create.name}`);
// cf https://github.com/typeorm/typeorm/issues/2924#issuecomment-430265476
const linkEntity = Object.assign( new LinkEntity(), link );
const linkEntity = Object.assign(new LinkEntity(), link);
linkEntity.id = uuidv4();
return await this.linkRepository.save(linkEntity).catch((error) => {
this.logger.error(`Error saving the link into the database.`, `${error}`, `${LinksService.name} - ${this.create.name}`);
......
......@@ -14,6 +14,7 @@ export class OrganizationDTO {
description: string;
@ApiModelPropertyOptional()
@IsOptional()
@IsString()
elasticSearchName?: string;
......
import { Entity, PrimaryColumn, Column, OneToMany, Generated, BeforeInsert } from 'typeorm';
import { Entity, PrimaryColumn, Column, OneToMany, Generated, BeforeInsert, BeforeUpdate } from 'typeorm';
import { LinkEntity } from '../links/link.entity';
import { ApiModelProperty } from '@nestjs/swagger';
import * as uuidv4 from 'uuid/v4';
@Entity('organization')
export class Organization {
......@@ -32,22 +31,4 @@ export class Organization {
@Column('text', { nullable: true })
@ApiModelProperty()
logo: string;
@BeforeInsert()
private beforeInsert() {
if (!this.id) {
this.id = uuidv4();
}
// UUIDs are not automatically created for related entities
// cf. https://github.com/typeorm/typeorm/issues/4209
if (this.links) {
this.links.forEach((link) => {
if (!link.id) {
link.id = uuidv4();
}
})
}
}
}
import { Injectable, Logger, InternalServerErrorException, NotFoundException, HttpService } from '@nestjs/common';
import { Injectable, Logger, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { Organization } from './organization.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { LinksService } from '../links/links.service';
import { OrganizationsRO, OrganizationDTO } from './organization.dto';
import { OrganizationRepository } from './organization.repository';
import * as uuidv4 from 'uuid/v4';
@Injectable()
export class OrganizationsService {
......@@ -97,11 +98,15 @@ export class OrganizationsService {
try {
this.logger.log('Entering function', `${OrganizationsService.name} - ${this.create.name}`);
// Only save the file name and not the full url of the logo
// organization.logo = await this._fileManagementService.upload('local-actors-logo', logoImage);
// cf https://github.com/typeorm/typeorm/issues/2924#issuecomment-430265476
const organizationEntity = Object.assign(new Organization(), organization);
organizationEntity.id = uuidv4();
organizationEntity.links.forEach((link) => {
if (!link.id) {
link.id = uuidv4();
}
});
const organizationCreated = await this.organizationRepository.save(organizationEntity).catch((error) => {
this.logger.error(`Error saving the organization into the database.`, `${error}`, `${OrganizationsService.name} - ${this.create.name}`);
......@@ -131,8 +136,12 @@ export class OrganizationsService {
throw new NotFoundException({ message: 'No item exists with the given id' });
}
let updated = Object.assign(new Organization(), toUpdate);
updated = Object.assign(updated, organization);
const updated = Object.assign(toUpdate, organization);
updated.links.forEach((link) => {
if (!link.id) {
link.id = uuidv4();
}
});
const saved = await this.organizationRepository.save(updated).catch((error) => {
this.logger.error(`Error while updating the organization.`, `${error}`, `${OrganizationsService.name} - ${this.update.name}`);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment