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
19 changes: 17 additions & 2 deletions client/src/entities/teacher.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {
ArrayInput,
DateField,
DateInput,
DateTimeInput,
email,
EmailField,
FunctionField,
maxLength,
ReferenceField,
required,
SimpleFormIterator,
TextField,
TextInput,
} from 'react-admin';
Expand Down Expand Up @@ -34,7 +38,14 @@ const Datagrid = ({ isAdmin, children, ...props }) => {
<TextField source="number" />
<TextField source="phone" />
<TextField source="phone2" />
<EmailField source="email" />
<FunctionField
source="email"
render={(record) => [].concat(record.email ?? []).map((addr) => (
<div key={addr}>
<EmailField record={{ email: addr }} source="email" />
</div>
))}
/>
Comment on lines +41 to +48
<TextField source="comment" />
{isAdmin && <DateField showDate showTime source="createdAt" />}
{isAdmin && <DateField showDate showTime source="updatedAt" />}
Expand All @@ -54,7 +65,11 @@ const Inputs = ({ isCreate, isAdmin }) => {
<TextInput source="number" validate={[maxLength(10), unique()]} />
<TextInput source="phone" validate={maxLength(10)} />
<TextInput source="phone2" validate={maxLength(10)} />
<TextInput source="email" validate={maxLength(500)} />
<ArrayInput source="email">
<SimpleFormIterator>
<TextInput source="" label={false} type="email" validate={email()} />
</SimpleFormIterator>
</ArrayInput>
<TextInput source="comment" validate={maxLength(1000)} />
{!isCreate && isAdmin && <DateTimeInput source="createdAt" disabled />}
{!isCreate && isAdmin && <DateTimeInput source="updatedAt" disabled />}
Expand Down
23 changes: 17 additions & 6 deletions server/src/db/entities/Teacher.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BeforeInsert, BeforeUpdate, Column, Entity, Index, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { IHasUserId } from '@shared/base-entity/interface';
import { User } from 'src/db/entities/User.entity';
import { IsOptional } from 'class-validator';
import { IsArray, IsEmail, IsOptional } from 'class-validator';
import { Transform } from 'class-transformer';
import { CrudValidationGroups } from '@dataui/crud';
import { IsNotEmpty, IsUniqueCombination, MaxLength } from '@shared/utils/validation/class-validator-he';
import { StringType } from '@shared/utils/entity/class-transformer';
Expand All @@ -17,8 +18,9 @@ import { CreatedAtColumn, UpdatedAtColumn } from '@shared/utils/entity/column-ty
export class Teacher implements IHasUserId {
@BeforeInsert()
@BeforeUpdate()
normalizeNumber() {
normalizeFields() {
if (this.number === '') this.number = null;
this.email = splitEmails(this.email);
}

@PrimaryGeneratedColumn({ type: 'int', name: 'id' })
Expand Down Expand Up @@ -65,10 +67,11 @@ export class Teacher implements IHasUserId {
number: string | null;

@IsOptional({ always: true })
@StringType
@MaxLength(500, { always: true })
@Column('varchar', { name: 'email', nullable: true, length: 500 })
email: string | null;
@IsArray({ always: true })
@IsEmail({}, { each: true, always: true })
@Transform(({ value }) => splitEmails(value))
@Column('simple-array', { name: 'email', nullable: true })
email: string[] | null;

@IsOptional({ always: true })
@StringType
Expand All @@ -95,3 +98,11 @@ export class Teacher implements IHasUserId {
@JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
user: User;
}

function splitEmails(value: unknown): string[] | null {
if (typeof value !== 'string') return value as string[] | null;
return value
.split(',')
.map((e) => e.trim())
.filter(Boolean);
}
Comment on lines +102 to +108
10 changes: 9 additions & 1 deletion server/src/entity-modules/__tests__/teacher.config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ describe('TeacherConfig', () => {
{ value: 'name', label: 'שם' },
{ value: 'phone', label: 'טלפון' },
{ value: 'phone2', label: 'טלפון 2' },
{ value: 'email', label: 'כתובת מייל' },
{ value: expect.any(Function), label: 'כתובת מייל' },
{ value: 'displayName', label: 'שם לתצוגה' },
]),
);
});

it('should join teacher emails into a comma separated string for export', () => {
const headers = teacherConfig.exporter.getExportHeaders([]);
const emailHeader = headers.find((h: any) => h.label === 'כתובת מייל') as any;

expect(emailHeader.value({ email: ['a@example.com', 'b@example.com'] })).toBe('a@example.com, b@example.com');
expect(emailHeader.value({ email: null })).toBe('');
});
});
2 changes: 1 addition & 1 deletion server/src/entity-modules/teacher.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function getConfig(): BaseEntityModuleOptions {
{ value: 'number', label: 'מספר מורה' },
{ value: 'phone', label: 'טלפון' },
{ value: 'phone2', label: 'טלפון 2' },
{ value: 'email', label: 'כתובת מייל' },
{ value: (record: any) => (record.email ?? []).join(', '), label: 'כתובת מייל' },
{ value: 'displayName', label: 'שם לתצוגה' },
];
},
Expand Down
18 changes: 18 additions & 0 deletions server/src/migrations/1788633251783-ConvertTeacherEmailToArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class ConvertTeacherEmailToArray1788633251783 implements MigrationInterface {
name = 'ConvertTeacherEmailToArray1788633251783'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE \`teachers\` MODIFY COLUMN \`email\` text NULL
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE \`teachers\` MODIFY COLUMN \`email\` varchar(500) NULL
`);
}

}
Loading