Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/backend/src/config/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AddAnimalUpdatesAndFosterTypeArray1793163582751 } from '../migrations/1
import { AddFarmAnimalsToFosterType1793476100199 } from '../migrations/1793476100199-AddFarmAnimalsToFosterType';
import { RenameAdminUsersToOwners1793485161630 } from '../migrations/1793485161630-RenameAdminUsersToOwners';
import { AddFosterTypePreferences1793600000000 } from '../migrations/1793600000000-AddFosterTypePreferences';
import { AddPetsAndHouseholdDetails1794408338094 } from '../migrations/1794408338094-AddPetsAndHouseholdDetails';
import { AddMatchAdditionalComments1794784721012 } from '../migrations/1794784721012-AddMatchAdditionalComments';

const schemaMigrations = [
Expand All @@ -29,6 +30,7 @@ const schemaMigrations = [
AddFarmAnimalsToFosterType1793476100199,
RenameAdminUsersToOwners1793485161630,
AddFosterTypePreferences1793600000000,
AddPetsAndHouseholdDetails1794408338094,
AddMatchAdditionalComments1794784721012,
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddPetsAndHouseholdDetails1794408338094
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TABLE "pets" (
"pet_id" SERIAL PRIMARY KEY,
"volunteer_id" int NOT NULL
REFERENCES "foster_volunteers" ("volunteer_id") ON DELETE CASCADE,
"age_year" int NOT NULL,
"age_month" int NOT NULL,
"species" "foster_type_enum" NOT NULL,
"breed" varchar(255) NOT NULL,
CONSTRAINT "CHK_pets_age_year" CHECK ("age_year" >= 0),
CONSTRAINT "CHK_pets_age_month" CHECK ("age_month" BETWEEN 0 AND 11)
)
`);

await queryRunner.query(`
ALTER TABLE "foster_volunteers"
ADD COLUMN "has_children" boolean NOT NULL DEFAULT false,
ADD COLUMN "children_ages" int[] NOT NULL DEFAULT '{}',
ADD COLUMN "has_separate_foster_room" boolean NOT NULL DEFAULT false,
ADD COLUMN "lively_neighborhood" boolean NOT NULL DEFAULT false,
DROP COLUMN "resident_animals"
`);

await queryRunner.query(`
ALTER TABLE "foster_volunteers"
ALTER COLUMN "has_children" DROP DEFAULT,
Comment thread
dburkhart07 marked this conversation as resolved.
Resolved
ALTER COLUMN "has_separate_foster_room" DROP DEFAULT,
ALTER COLUMN "lively_neighborhood" DROP DEFAULT,
ADD CONSTRAINT "CHK_foster_volunteers_children_ages" CHECK (
("has_children" AND cardinality("children_ages") > 0)
OR (NOT "has_children" AND cardinality("children_ages") = 0)
),
ADD CONSTRAINT "CHK_foster_volunteers_children_ages_nonnegative"
CHECK (0 <= ALL ("children_ages"))
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "foster_volunteers"
DROP CONSTRAINT "CHK_foster_volunteers_children_ages_nonnegative",
DROP CONSTRAINT "CHK_foster_volunteers_children_ages",
DROP COLUMN "lively_neighborhood",
DROP COLUMN "has_separate_foster_room",
DROP COLUMN "children_ages",
DROP COLUMN "has_children",
ADD COLUMN "resident_animals" text NOT NULL DEFAULT ''
`);

await queryRunner.query(`
ALTER TABLE "foster_volunteers" ALTER COLUMN "resident_animals" DROP DEFAULT
Comment thread
dburkhart07 marked this conversation as resolved.
Resolved
`);

await queryRunner.query(`DROP TABLE "pets"`);
}
}
21 changes: 21 additions & 0 deletions apps/backend/src/pets/dtos/pet.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { IsEnum, IsInt, Max, MaxLength, Min } from 'class-validator';
Comment thread
Juwang110 marked this conversation as resolved.
Resolved
import { FosterType } from '../../volunteers/volunteers.types';
import { IsNonEmptyString } from '../../utils/decorators/is-non-empty-string.decorator';

export class PetDto {
@IsInt()
@Min(0)
ageYear!: number;

@IsInt()
@Min(0)
@Max(11)
ageMonth!: number;

@IsEnum(FosterType)
species!: FosterType;

@IsNonEmptyString()
@MaxLength(255)
breed!: string;
}
41 changes: 41 additions & 0 deletions apps/backend/src/pets/pets.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
ManyToOne,
JoinColumn,
Check,
} from 'typeorm';
import { FosterVolunteer } from '../volunteers/volunteers.entity';
import { FosterType } from '../volunteers/volunteers.types';

@Entity('pets')
@Check('CHK_pets_age_year', '"age_year" >= 0')
@Check('CHK_pets_age_month', '"age_month" BETWEEN 0 AND 11')
export class Pet {
@PrimaryGeneratedColumn({ name: 'pet_id' })
petId!: number;

@ManyToOne(() => FosterVolunteer, (volunteer) => volunteer.pets, {
nullable: false,
onDelete: 'CASCADE',
orphanedRowAction: 'delete',
})
@JoinColumn({ name: 'volunteer_id' })
volunteer!: FosterVolunteer;

@Column({ name: 'volunteer_id' })
volunteerId!: number;

@Column({ name: 'age_year', type: 'int' })
ageYear!: number;

@Column({ name: 'age_month', type: 'int' })
ageMonth!: number;

@Column({ type: 'enum', enum: FosterType, enumName: 'foster_type_enum' })
species!: FosterType;

@Column({ type: 'varchar', length: 255 })
breed!: string;
}
29 changes: 26 additions & 3 deletions apps/backend/src/volunteers/dtos/create-volunteer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import {
ArrayNotEmpty,
ArrayUnique,
IsArray,
IsBoolean,
IsEmail,
IsEnum,
IsInt,
IsNotEmpty,
IsPhoneNumber,
IsString,
IsStrongPassword,
MaxLength,
Min,
ValidateIf,
ValidateNested,
} from 'class-validator';
import { Transform } from 'class-transformer';
import { Transform, Type } from 'class-transformer';
import { IsNonEmptyString } from '../../utils/decorators/is-non-empty-string.decorator';
import { Homebase } from '../../types';
import { FosterTraining, FosterType } from '../volunteers.types';
import { PetDto } from '../../pets/dtos/pet.dto';

export class CreateVolunteerDto {
@IsNonEmptyString()
Expand Down Expand Up @@ -70,8 +75,26 @@ export class CreateVolunteerDto {
@IsEnum(Homebase)
homebase!: Homebase;

@IsNonEmptyString()
residentAnimals!: string;
@IsBoolean()
hasChildren!: boolean;

@ValidateIf((_, value) => value !== undefined)
@IsArray()
@IsInt({ each: true })
@Min(0, { each: true })
childrenAges?: number[];

@IsBoolean()
hasSeparateFosterRoom!: boolean;

@IsBoolean()
livelyNeighborhood!: boolean;

@ValidateIf((_, value) => value !== undefined)
@IsArray()
@ValidateNested({ each: true })
@Type(() => PetDto)
pets?: PetDto[];

@ValidateIf((_, value) => value !== undefined && value !== null)
@IsNonEmptyString()
Expand Down
35 changes: 32 additions & 3 deletions apps/backend/src/volunteers/dtos/update-volunteer.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import {
IsArray,
IsBoolean,
IsEnum,
IsInt,
IsNotEmpty,
MaxLength,
Min,
IsPhoneNumber,
IsEmail,
ArrayUnique,
ValidateIf,
ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { IsNonEmptyString } from '../../utils/decorators/is-non-empty-string.decorator';
import { PetDto } from '../../pets/dtos/pet.dto';
import { AtLeastOneValue } from './validators/at-least-one-value.validator';

@AtLeastOneValue<UpdateVolunteerDto>([
Expand All @@ -23,7 +28,11 @@ import { AtLeastOneValue } from './validators/at-least-one-value.validator';
'address',
'city',
'zipcode',
'residentAnimals',
'hasChildren',
'childrenAges',
'hasSeparateFosterRoom',
'livelyNeighborhood',
'pets',
'notes',
'fosterType',
'animalUpdates',
Expand Down Expand Up @@ -71,8 +80,28 @@ export class UpdateVolunteerDto {
zipcode?: string;

@ValidateIf((_, value) => value !== undefined)
@IsNonEmptyString()
residentAnimals?: string;
@IsBoolean()
hasChildren?: boolean;

@ValidateIf((_, value) => value !== undefined)
@IsArray()
@IsInt({ each: true })
@Min(0, { each: true })
childrenAges?: number[];

@ValidateIf((_, value) => value !== undefined)
@IsBoolean()
hasSeparateFosterRoom?: boolean;

@ValidateIf((_, value) => value !== undefined)
@IsBoolean()
livelyNeighborhood?: boolean;

@ValidateIf((_, value) => value !== undefined)
@IsArray()
@ValidateNested({ each: true })
@Type(() => PetDto)
pets?: PetDto[];

@ValidateIf((_, value) => value !== undefined && value !== null)
@IsNonEmptyString()
Expand Down
24 changes: 23 additions & 1 deletion apps/backend/src/volunteers/volunteers.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { UpdateSpeciesPreferencesDto } from './dtos/update-species-preferences.d
import { CoordinatorUpdateVolunteerDto } from './dtos/coordinator-update-volunteer.dto';
import { Homebase } from '../types';
import { UpdateHomebaseDto } from './dtos/update-homebase.dto';
import { PetDto } from '../pets/dtos/pet.dto';

describe('VolunteersController', () => {
let controller: VolunteersController;
Expand Down Expand Up @@ -163,7 +164,9 @@ describe('VolunteersController', () => {
city: 'Boston',
zipcode: '02130',
homebase: Homebase.BOSTON,
residentAnimals: 'One cat',
hasChildren: false,
hasSeparateFosterRoom: true,
livelyNeighborhood: false,
fosterType: [FosterType.CAT],
password: 'Str0ng!Password',
} as CreateVolunteerDto;
Expand Down Expand Up @@ -224,6 +227,24 @@ describe('VolunteersController', () => {

expect(result).not.toHaveProperty('completedTrainings');
});

const pet = {
Comment thread
Juwang110 marked this conversation as resolved.
Resolved
ageYear: 3,
ageMonth: 11,
species: FosterType.CAT,
breed: 'Tabby',
} as PetDto;

it('accepts a volunteer with pets and children', async () => {
const result = await validationPipe.transform(
{ ...dto, hasChildren: true, childrenAges: [4, 9], pets: [pet] },
{ type: 'body', metatype: CreateVolunteerDto },
);

expect(result.hasChildren).toBe(true);
expect(result.childrenAges).toEqual([4, 9]);
expect(result.pets).toEqual([pet]);
});
});

describe('confirm', () => {
Expand Down Expand Up @@ -301,6 +322,7 @@ describe('VolunteersController', () => {
const result = await controller.getVolunteerById(1);

expect(result).toEqual(mockVolunteer);
expect(mockVolunteersService.findByIdOrFail).toHaveBeenCalledTimes(1);
expect(mockVolunteersService.findByIdOrFail).toHaveBeenCalledWith(1);
});

Expand Down
12 changes: 11 additions & 1 deletion apps/backend/src/volunteers/volunteers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ export class VolunteersController {
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'Invalid ID or invalid field',
description:
'Invalid ID, invalid field, no fields provided, or childrenAges does not match hasChildren',
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
Expand Down Expand Up @@ -216,6 +217,15 @@ export class VolunteersController {
description: 'The volunteer was updated',
type: VolunteerWithoutPrivateFields,
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description:
'Invalid ID, invalid field, no fields provided, or childrenAges does not match hasChildren',
})
@ApiResponse({
status: HttpStatus.NOT_FOUND,
description: 'The volunteer does not exist',
})
async updateVolunteerById(
@Param('volunteerId', ParseIntPipe) volunteerId: number,
@Body() dto: UpdateVolunteerDto,
Expand Down
31 changes: 29 additions & 2 deletions apps/backend/src/volunteers/volunteers.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ManyToMany,
JoinTable,
Unique,
Check,
} from 'typeorm';
import { Homebase } from '../types';
import {
Expand All @@ -14,10 +15,19 @@ import {
VolunteerStatus,
} from './volunteers.types';
import { FosterCoordinator } from '../coordinators/coordinators.entity';
import { Pet } from '../pets/pets.entity';
import { Match } from '../matches/matches.entity';

@Entity('foster_volunteers')
@Unique('UQ_foster_volunteers_email', ['email'])
@Check(
'CHK_foster_volunteers_children_ages',
'("has_children" AND cardinality("children_ages") > 0) OR (NOT "has_children" AND cardinality("children_ages") = 0)',
)
@Check(
'CHK_foster_volunteers_children_ages_nonnegative',
'0 <= ALL ("children_ages")',
)
export class FosterVolunteer {
@PrimaryGeneratedColumn({ name: 'volunteer_id' })
volunteerId!: number;
Expand Down Expand Up @@ -54,8 +64,25 @@ export class FosterVolunteer {
@Column({ type: 'enum', enum: Homebase })
homebase!: Homebase;

@Column({ name: 'resident_animals', type: 'text' })
residentAnimals!: string;
@Column({ name: 'has_children', type: 'boolean' })
hasChildren!: boolean;

@Column({
name: 'children_ages',
type: 'int',
array: true,
default: () => "'{}'",
})
childrenAges!: number[];

@Column({ name: 'has_separate_foster_room', type: 'boolean' })
hasSeparateFosterRoom!: boolean;

@Column({ name: 'lively_neighborhood', type: 'boolean' })
livelyNeighborhood!: boolean;

@OneToMany(() => Pet, (pet) => pet.volunteer, { cascade: true })
pets!: Pet[];
Comment thread
Juwang110 marked this conversation as resolved.
Resolved

@Column({ type: 'text', nullable: true })
notes!: string | null;
Expand Down
Loading
Loading