Intruduced a new folder structure for action models

ref #76
This commit is contained in:
Nicolai Ort 2021-01-10 16:10:02 +01:00
parent ee9df21ae5
commit 3bc172e7e0
31 changed files with 291 additions and 291 deletions

View File

@ -3,7 +3,7 @@ import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { getConnectionManager, Repository } from 'typeorm'; import { getConnectionManager, Repository } from 'typeorm';
import { DonorIdsNotMatchingError, DonorNotFoundError } from '../errors/DonorErrors'; import { DonorIdsNotMatchingError, DonorNotFoundError } from '../errors/DonorErrors';
import { CreateDonor } from '../models/actions/CreateDonor'; import { CreateDonor } from '../models/actions/CreateDonor';
import { UpdateDonor } from '../models/actions/UpdateDonor'; import { UpdateDonor } from '../models/actions/update/UpdateDonor';
import { Donor } from '../models/entities/Donor'; import { Donor } from '../models/entities/Donor';
import { ResponseDonor } from '../models/responses/ResponseDonor'; import { ResponseDonor } from '../models/responses/ResponseDonor';
import { ResponseEmpty } from '../models/responses/ResponseEmpty'; import { ResponseEmpty } from '../models/responses/ResponseEmpty';

View File

@ -1,6 +1,6 @@
import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator'; import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator';
import { config } from '../../config'; import { config } from '../../../config';
import { Address } from '../entities/Address'; import { Address } from '../../entities/Address';
/** /**
* This classed is used to create a new Address entity from a json body (post request). * This classed is used to create a new Address entity from a json body (post request).

View File

@ -1,85 +1,85 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { config } from '../../config'; import { config } from '../../config';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
import { GroupContact } from '../entities/GroupContact'; import { GroupContact } from '../entities/GroupContact';
/** /**
* This classed is used to create a new Group entity from a json body (post request). * This classed is used to create a new Group entity from a json body (post request).
*/ */
export class CreateGroupContact { export class CreateGroupContact {
/** /**
* The new contact's first name. * The new contact's first name.
*/ */
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
firstname: string; firstname: string;
/** /**
* The new contact's middle name. * The new contact's middle name.
*/ */
@IsOptional() @IsOptional()
@IsString() @IsString()
middlename?: string; middlename?: string;
/** /**
* The new contact's last name. * The new contact's last name.
*/ */
@IsNotEmpty() @IsNotEmpty()
@IsString() @IsString()
lastname: string; lastname: string;
/** /**
* The new contact's address. * The new contact's address.
* Must be the address's id. * Must be the address's id.
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
address?: number; address?: number;
/** /**
* The contact's phone number. * The contact's phone number.
* This will be validated against the configured country phone numer syntax (default: international). * This will be validated against the configured country phone numer syntax (default: international).
*/ */
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**
* The contact's email address. * The contact's email address.
*/ */
@IsOptional() @IsOptional()
@IsEmail() @IsEmail()
email?: string; email?: string;
/** /**
* Gets the new contact's address by it's id. * Gets the new contact's address by it's id.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined || this.address === null) { if (this.address === undefined || this.address === null) {
return null; return null;
} }
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }
return address; return address;
} }
throw new AddressWrongTypeError; throw new AddressWrongTypeError;
} }
/** /**
* Creates a new Address entity from this. * Creates a new Address entity from this.
*/ */
public async toGroupContact(): Promise<GroupContact> { public async toGroupContact(): Promise<GroupContact> {
let contact: GroupContact = new GroupContact(); let contact: GroupContact = new GroupContact();
contact.firstname = this.firstname; contact.firstname = this.firstname;
contact.middlename = this.middlename; contact.middlename = this.middlename;
contact.lastname = this.lastname; contact.lastname = this.lastname;
contact.email = this.email; contact.email = this.email;
contact.phone = this.phone; contact.phone = this.phone;
contact.address = await this.getAddress(); contact.address = await this.getAddress();
return null; return null;
} }
} }

View File

@ -1,72 +1,72 @@
import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator'; import { IsEmail, IsInt, IsNotEmpty, IsOptional, IsPhoneNumber, IsString } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import { config } from '../../config'; import { config } from '../../config';
import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors'; import { AddressNotFoundError, AddressWrongTypeError } from '../../errors/AddressErrors';
import { Address } from '../entities/Address'; import { Address } from '../entities/Address';
/** /**
* This classed is used to create a new Participant entity from a json body (post request). * This classed is used to create a new Participant entity from a json body (post request).
*/ */
export abstract class CreateParticipant { export abstract class CreateParticipant {
/** /**
* The new participant's first name. * The new participant's first name.
*/ */
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
firstname: string; firstname: string;
/** /**
* The new participant's middle name. * The new participant's middle name.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
middlename?: string; middlename?: string;
/** /**
* The new participant's last name. * The new participant's last name.
*/ */
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
lastname: string; lastname: string;
/** /**
* The new participant's phone number. * The new participant's phone number.
* This will be validated against the configured country phone numer syntax (default: international). * This will be validated against the configured country phone numer syntax (default: international).
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
phone?: string; phone?: string;
/** /**
* The new participant's e-mail address. * The new participant's e-mail address.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
@IsEmail() @IsEmail()
email?: string; email?: string;
/** /**
* The new participant's address. * The new participant's address.
* Must be of type number (address id). * Must be of type number (address id).
*/ */
@IsInt() @IsInt()
@IsOptional() @IsOptional()
address?: number; address?: number;
/** /**
* Gets the new participant's address by it's address. * Gets the new participant's address by it's address.
*/ */
public async getAddress(): Promise<Address> { public async getAddress(): Promise<Address> {
if (this.address === undefined || this.address === null) { if (this.address === undefined || this.address === null) {
return null; return null;
} }
if (!isNaN(this.address)) { if (!isNaN(this.address)) {
let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address }); let address = await getConnectionManager().get().getRepository(Address).findOne({ id: this.address });
if (!address) { throw new AddressNotFoundError; } if (!address) { throw new AddressNotFoundError; }
return address; return address;
} }
throw new AddressWrongTypeError; throw new AddressWrongTypeError;
} }
} }

View File

@ -1,132 +1,132 @@
import * as argon2 from "argon2"; import * as argon2 from "argon2";
import { IsBoolean, IsEmail, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator'; import { IsBoolean, IsEmail, IsOptional, IsPhoneNumber, IsString, IsUrl } from 'class-validator';
import { getConnectionManager } from 'typeorm'; import { getConnectionManager } from 'typeorm';
import * as uuid from 'uuid'; import * as uuid from 'uuid';
import { config } from '../../config'; import { config } from '../../config';
import { UsernameOrEmailNeededError } from '../../errors/UserErrors'; import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
import { UserGroupNotFoundError } from '../../errors/UserGroupErrors'; import { UserGroupNotFoundError } from '../../errors/UserGroupErrors';
import { User } from '../entities/User'; import { User } from '../entities/User';
import { UserGroup } from '../entities/UserGroup'; import { UserGroup } from '../entities/UserGroup';
/** /**
* This classed is used to create a new User entity from a json body (post request). * This classed is used to create a new User entity from a json body (post request).
*/ */
export class CreateUser { export class CreateUser {
/** /**
* The new user's first name. * The new user's first name.
*/ */
@IsString() @IsString()
firstname: string; firstname: string;
/** /**
* The new user's middle name. * The new user's middle name.
*/ */
@IsString() @IsString()
@IsOptional() @IsOptional()
middlename?: string; middlename?: string;
/** /**
* The new user's last name. * The new user's last name.
*/ */
@IsString() @IsString()
lastname: string; lastname: string;
/** /**
* The new user's username. * The new user's username.
* You have to provide at least one of: {email, username}. * You have to provide at least one of: {email, username}.
*/ */
@IsOptional() @IsOptional()
@IsString() @IsString()
username?: string; username?: string;
/** /**
* The new user's email address. * The new user's email address.
* You have to provide at least one of: {email, username}. * You have to provide at least one of: {email, username}.
*/ */
@IsEmail() @IsEmail()
@IsString() @IsString()
@IsOptional() @IsOptional()
email?: string; email?: string;
/** /**
* The new user's phone number. * The new user's phone number.
* This will be validated against the configured country phone numer syntax (default: international). * This will be validated against the configured country phone numer syntax (default: international).
*/ */
@IsPhoneNumber(config.phone_validation_countrycode) @IsPhoneNumber(config.phone_validation_countrycode)
@IsOptional() @IsOptional()
phone?: string; phone?: string;
/** /**
* The new user's password. * The new user's password.
* This will of course not be saved in plaintext :) * This will of course not be saved in plaintext :)
*/ */
@IsString() @IsString()
password: string; password: string;
/** /**
* Will the new user be enabled from the start? * Will the new user be enabled from the start?
* Default: true * Default: true
*/ */
@IsBoolean() @IsBoolean()
@IsOptional() @IsOptional()
enabled?: boolean = true; enabled?: boolean = true;
/** /**
* The new user's groups' id(s). * The new user's groups' id(s).
* You can provide either one groupId or an array of groupIDs. * You can provide either one groupId or an array of groupIDs.
*/ */
@IsOptional() @IsOptional()
groups?: number[] | number groups?: number[] | number
/** /**
* The user's profile pic (or rather a url pointing to it). * The user's profile pic (or rather a url pointing to it).
*/ */
@IsString() @IsString()
@IsUrl() @IsUrl()
@IsOptional() @IsOptional()
profilePic?: string; profilePic?: string;
/** /**
* Converts this to a User entity. * Converts this to a User entity.
*/ */
public async toUser(): Promise<User> { public async toUser(): Promise<User> {
let newUser: User = new User(); let newUser: User = new User();
if (this.email === undefined && this.username === undefined) { if (this.email === undefined && this.username === undefined) {
throw new UsernameOrEmailNeededError(); throw new UsernameOrEmailNeededError();
} }
newUser.email = this.email newUser.email = this.email
newUser.username = this.username newUser.username = this.username
newUser.firstname = this.firstname newUser.firstname = this.firstname
newUser.middlename = this.middlename newUser.middlename = this.middlename
newUser.lastname = this.lastname newUser.lastname = this.lastname
newUser.uuid = uuid.v4() newUser.uuid = uuid.v4()
newUser.phone = this.phone newUser.phone = this.phone
newUser.password = await argon2.hash(this.password + newUser.uuid); newUser.password = await argon2.hash(this.password + newUser.uuid);
newUser.groups = await this.getGroups(); newUser.groups = await this.getGroups();
newUser.enabled = this.enabled; newUser.enabled = this.enabled;
if (!this.profilePic) { newUser.profilePic = `https://dev.lauf-fuer-kaya.de/lfk-logo.png`; } if (!this.profilePic) { newUser.profilePic = `https://dev.lauf-fuer-kaya.de/lfk-logo.png`; }
else { newUser.profilePic = this.profilePic; } else { newUser.profilePic = this.profilePic; }
return newUser; return newUser;
} }
/** /**
* Get's all groups for this user by their id's; * Get's all groups for this user by their id's;
*/ */
public async getGroups() { public async getGroups() {
if (!this.groups) { return null; } if (!this.groups) { return null; }
let groups = new Array<UserGroup>(); let groups = new Array<UserGroup>();
if (!Array.isArray(this.groups)) { if (!Array.isArray(this.groups)) {
this.groups = [this.groups] this.groups = [this.groups]
} }
for (let group of this.groups) { for (let group of this.groups) {
let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group }); let found = await getConnectionManager().get().getRepository(UserGroup).findOne({ id: group });
if (!found) { throw new UserGroupNotFoundError(); } if (!found) { throw new UserGroupNotFoundError(); }
groups.push(found); groups.push(found);
} }
return groups; return groups;
} }
} }

View File

@ -1,6 +1,6 @@
import { IsBoolean, IsInt, IsOptional } from 'class-validator'; import { IsBoolean, IsInt, IsOptional } from 'class-validator';
import { DonorReceiptAddressNeededError } from '../../errors/DonorErrors'; import { DonorReceiptAddressNeededError } from '../../../errors/DonorErrors';
import { Donor } from '../entities/Donor'; import { Donor } from '../../entities/Donor';
import { CreateParticipant } from './CreateParticipant'; import { CreateParticipant } from './CreateParticipant';
/** /**