import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm';
import { GroupContactNotFoundError } from '../../../errors/GroupContactErrors';
import { GroupContact } from '../../entities/GroupContact';

/**
 * This classed is used to create a new RunnerGroup entity from a json body (post request).
 */
export abstract class CreateRunnerGroup {
    /**
     * The new group's name.
     */
    @IsNotEmpty()
    @IsString()
    name: string;

    /**
     * The new group's contact's id.
     * Optional
     */
    @IsInt()
    @IsOptional()
    contact?: number;

    /**
     * Gets the new group's contact by it's id.
     */
    public async getContact(): Promise<GroupContact> {
        if (!this.contact) { return null; }
        let contact = await getConnectionManager().get().getRepository(GroupContact).findOne({ id: this.contact });
        if (!contact) { throw new GroupContactNotFoundError; }
        return contact;

    }
}