98
									
								
								src/models/actions/update/UpdateGroupContact.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								src/models/actions/update/UpdateGroupContact.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
import { IsEmail, IsNotEmpty, IsObject, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
 | 
			
		||||
import { getConnectionManager } from 'typeorm';
 | 
			
		||||
import { config } from '../../../config';
 | 
			
		||||
import { RunnerGroupNotFoundError } from '../../../errors/RunnerGroupErrors';
 | 
			
		||||
import { Address } from '../../entities/Address';
 | 
			
		||||
import { GroupContact } from '../../entities/GroupContact';
 | 
			
		||||
import { RunnerGroup } from '../../entities/RunnerGroup';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This classed is used to create a new Group entity from a json body (post request).
 | 
			
		||||
 */
 | 
			
		||||
export class UpdateGroupContact {
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contact's first name.
 | 
			
		||||
     */
 | 
			
		||||
    @IsNotEmpty()
 | 
			
		||||
    @IsString()
 | 
			
		||||
    firstname: string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contact's middle name.
 | 
			
		||||
     */
 | 
			
		||||
    @IsOptional()
 | 
			
		||||
    @IsString()
 | 
			
		||||
    middlename?: string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contact's last name.
 | 
			
		||||
     */
 | 
			
		||||
    @IsNotEmpty()
 | 
			
		||||
    @IsString()
 | 
			
		||||
    lastname: string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contact's address.
 | 
			
		||||
     */
 | 
			
		||||
    @IsOptional()
 | 
			
		||||
    @IsObject()
 | 
			
		||||
    address?: Address;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contact's phone number.
 | 
			
		||||
     * This will be validated against the configured country phone numer syntax (default: international).
 | 
			
		||||
     */
 | 
			
		||||
    @IsOptional()
 | 
			
		||||
    @IsPhoneNumber(config.phone_validation_countrycode)
 | 
			
		||||
    phone?: string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contact's email address.
 | 
			
		||||
     */
 | 
			
		||||
    @IsOptional()
 | 
			
		||||
    @IsEmail()
 | 
			
		||||
    email?: string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The updated contacts's groups' ids.
 | 
			
		||||
     * You can provide either one groupId or an array of groupIDs.
 | 
			
		||||
     */
 | 
			
		||||
    @IsOptional()
 | 
			
		||||
    groups?: number[] | number
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get's all groups for this contact by their id's;
 | 
			
		||||
     */
 | 
			
		||||
    public async getGroups(): Promise<RunnerGroup[]> {
 | 
			
		||||
        if (!this.groups) { return null; }
 | 
			
		||||
        let groups = new Array<RunnerGroup>();
 | 
			
		||||
        if (!Array.isArray(this.groups)) {
 | 
			
		||||
            this.groups = [this.groups]
 | 
			
		||||
        }
 | 
			
		||||
        for (let group of this.groups) {
 | 
			
		||||
            let found = await getConnectionManager().get().getRepository(RunnerGroup).findOne({ id: group });
 | 
			
		||||
            if (!found) { throw new RunnerGroupNotFoundError(); }
 | 
			
		||||
            groups.push(found);
 | 
			
		||||
        }
 | 
			
		||||
        return groups;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Updates a provided Donor entity based on this.
 | 
			
		||||
     * @param contact the contact you want to update.
 | 
			
		||||
     */
 | 
			
		||||
    public async update(contact: GroupContact): Promise<GroupContact> {
 | 
			
		||||
        contact.firstname = this.firstname; GroupContact
 | 
			
		||||
        contact.middlename = this.middlename;
 | 
			
		||||
        contact.lastname = this.lastname;
 | 
			
		||||
        contact.phone = this.phone;
 | 
			
		||||
        contact.email = this.email;
 | 
			
		||||
        if (!this.address) { contact.address.reset(); }
 | 
			
		||||
        else { contact.address = this.address; }
 | 
			
		||||
        Address.validate(contact.address);
 | 
			
		||||
        contact.groups = await this.getGroups();
 | 
			
		||||
 | 
			
		||||
        return contact;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user