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
3 changes: 3 additions & 0 deletions client/src/domainTranslations.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,12 @@ export default {
dayOfWeek: 'יום בשבוע',
weeksBack: 'שבועות אחורה',
active: 'פעיל?',
lastRunAt: 'הרצה אחרונה',
lastRunSummary: 'תוצאת הרצה אחרונה',
},
actions: {
runNow: 'הרץ עכשיו',
preview: 'מי תימחק? (תצוגה מקדימה)',
},
},
grade_effect_by_user: {
Expand Down
55 changes: 53 additions & 2 deletions client/src/entities/attendance-cleanup-rule.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useState } from 'react';
import {
BooleanField,
Button,
BooleanInput,
DateField,
FunctionField,
Expand All @@ -12,14 +14,21 @@ import {
SelectInput,
TextField,
TextInput,
useDataProvider,
useListContext,
useNotify,
useRefresh,
} from 'react-admin';
import { Dialog, DialogActions, DialogContent, DialogTitle, Typography } from '@mui/material';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import VisibilityIcon from '@mui/icons-material/Visibility';
import { handleError } from '@shared/utils/notifyUtil';
import { CommonDatagrid } from '@shared/components/crudContainers/CommonList';
import { CommonRepresentation } from '@shared/components/CommonRepresentation';
import { getResourceComponents } from '@shared/components/crudContainers/CommonEntity';
import CommonReferenceInput from '@shared/components/fields/CommonReferenceInput';
import { CommonReferenceInputFilter, filterByUserId } from '@shared/components/fields/CommonReferenceInputFilter';
import { commonAdminFilters } from '@shared/components/fields/PermissionFilter';
import { BulkActionButton } from '@shared/components/crudContainers/BulkActionButton';

const dayOfWeekChoices = [
{ id: 0, name: 'ראשון' },
Expand All @@ -37,9 +46,49 @@ const filters = [
<CommonReferenceInputFilter source="lessonReferenceId" reference="lesson" dynamicFilter={filterByUserId} />,
];

// Runs a rule action on the selected rules and shows the per-rule result in a dialog
// (a notification is too short-lived for a list of dates and student names).
const RuleResultButton = ({ label, name, icon }) => {
const dataProvider = useDataProvider();
const { selectedIds, resource } = useListContext();
const notify = useNotify();
const refresh = useRefresh();
const [result, setResult] = useState(null);
const [isLoading, setIsLoading] = useState(false);

const handleClick = async () => {
setIsLoading(true);
try {
const response = await dataProvider.action(resource, name, { 'extra.ids': selectedIds });
setResult(typeof response.body === 'string' ? response.body : JSON.stringify(response.body));
refresh();
} catch (error) {
handleError(notify)(error);
} finally {
setIsLoading(false);
}
};

return (
<>
<Button label={label} onClick={handleClick} disabled={isLoading}>{icon}</Button>
<Dialog open={result !== null} onClose={() => setResult(null)} fullWidth maxWidth="md">
<DialogTitle>{label}</DialogTitle>
<DialogContent>
<Typography sx={{ whiteSpace: 'pre-line' }}>{result}</Typography>
</DialogContent>
<DialogActions>
<Button label="ra.action.close" onClick={() => setResult(null)} />
</DialogActions>
</Dialog>
</>
);
};

const Datagrid = ({ isAdmin, children, ...props }) => {
const additionalBulkButtons = [
<BulkActionButton key="runNow" label="הרץ עכשיו" name="runNow" reloadOnEnd />,
<RuleResultButton key="preview" label="מי תימחק? (תצוגה מקדימה)" name="preview" icon={<VisibilityIcon />} />,
<RuleResultButton key="runNow" label="הרץ עכשיו" name="runNow" icon={<PlayArrowIcon />} />,
];

return (
Expand All @@ -55,6 +104,8 @@ const Datagrid = ({ isAdmin, children, ...props }) => {
/>
<NumberField source="weeksBack" />
<BooleanField source="active" />
<DateField showDate showTime source="lastRunAt" />
<TextField source="lastRunSummary" />
{isAdmin && <DateField showDate showTime source="createdAt" />}
</CommonDatagrid>
);
Expand Down
1 change: 1 addition & 0 deletions client/src/roadmapFeatures.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions server/src/db/entities/AttendanceCleanupRule.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ export class AttendanceCleanupRule implements IHasUserId {
@Column('boolean', { name: 'active', default: true })
active: boolean;

/** Set by the sweep after each real run - read-only for the client. */
@IsOptional({ always: true })
@Column('datetime', { name: 'last_run_at', nullable: true })
lastRunAt: Date | null;

/** What the last run did: how many att_reports it deleted and which dates it skipped, and why. */
@IsOptional({ always: true })
@Column('text', { name: 'last_run_summary', nullable: true })
lastRunSummary: string | null;

@CreatedAtColumn()
createdAt: Date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { ADMIN_FILTER, CrudAuthFilter } from '@shared/auth/crud-auth.filter';
import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.entity';
import attendanceCleanupRuleConfig from '../attendance-cleanup-rule.config';

const mockRunCleanupRules = jest.fn();
jest.mock('src/jobs/attendance-cleanup-sweep.handler', () => ({
runCleanupRules: (...args: any[]) => mockRunCleanupRules(...args),
}));

describe('AttendanceCleanupRuleConfig', () => {
it('should have correct base configuration', () => {
expect(attendanceCleanupRuleConfig.entity).toBe(AttendanceCleanupRule);
Expand Down Expand Up @@ -34,7 +39,7 @@ describe('AttendanceCleanupRuleConfig', () => {
});
});

describe('doAction: runNow', () => {
describe('doAction: runNow / preview', () => {
function makeService() {
const ServiceClass = attendanceCleanupRuleConfig.service as any;
const mockRepository = {
Expand All @@ -46,63 +51,83 @@ describe('AttendanceCleanupRuleConfig', () => {
targetName: 'AttendanceCleanupRule',
},
} as any;
const jobService = { enqueue: jest.fn().mockResolvedValue({ id: 42 }) };
const service = new ServiceClass(mockRepository, {} as any, jobService);
return { service, jobService };
const service = new ServiceClass(mockRepository, {} as any);
service.dataSource = { name: 'ds' };
return { service };
}

function makeReq(ids: string) {
function makeReq(ids: string, action = 'runNow') {
// getManyByIds is what actually applies crudAuth's filter (via createBuilder) - it's
// mocked here rather than re-testing that plumbing, which base-entity.service.spec.ts
// already covers directly.
const req = {
auth: { id: -1, permissions: { admin: true } },
parsed: { extra: { action: 'runNow', ids } },
parsed: { extra: { action, ids } },
options: {},
} as unknown as CrudRequest;
return req;
}

it("enqueues a job scoped to the resolved rules' own userId", async () => {
const { service, jobService } = makeService();
jest.spyOn(service, 'getManyByIds').mockResolvedValue([
beforeEach(() => {
mockRunCleanupRules.mockReset();
mockRunCleanupRules.mockImplementation(async (_ds, _userId, rules) => ({
totalDeleted: rules.length,
perRule: rules.map((rule) => `כלל ${rule.id}: נמחקו 1`),
}));
});

it("runs the resolved rules under their own userId and returns the summary", async () => {
const { service } = makeService();
const rules = [
{ id: 1, userId: 38 },
{ id: 2, userId: 38 },
] as any);
];
jest.spyOn(service, 'getManyByIds').mockResolvedValue(rules as any);
const req = makeReq('1,2');

const message = await service.doAction(req, {});

expect(service.getManyByIds).toHaveBeenCalledWith(req, [1, 2]);
expect(jobService.enqueue).toHaveBeenCalledTimes(1);
expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1, 2] });
expect(message).toContain('42');
expect(mockRunCleanupRules).toHaveBeenCalledTimes(1);
expect(mockRunCleanupRules).toHaveBeenCalledWith(service.dataSource, 38, rules, false);
expect(message).toContain('נמחקו 2');
expect(message).toContain('כלל 1: נמחקו 1');
});

it('enqueues nothing when none of the selected ids resolve for this caller', async () => {
const { service, jobService } = makeService();
it('runs nothing when none of the selected ids resolve for this caller', async () => {
const { service } = makeService();
jest.spyOn(service, 'getManyByIds').mockResolvedValue([]);
const req = makeReq('99'); // e.g. owned by someone else, filtered out by crudAuth

const message = await service.doAction(req, {});

expect(jobService.enqueue).not.toHaveBeenCalled();
expect(mockRunCleanupRules).not.toHaveBeenCalled();
expect(message).toContain('0');
});

it('enqueues one job per distinct owner when the resolved rules span multiple users', async () => {
const { service, jobService } = makeService();
it('runs once per distinct owner when the resolved rules span multiple users', async () => {
const { service } = makeService();
jest.spyOn(service, 'getManyByIds').mockResolvedValue([
{ id: 1, userId: 38 },
{ id: 2, userId: 39 },
] as any);
const req = makeReq('1,2');

await service.doAction(req, {});
await service.doAction(makeReq('1,2'), {});

expect(mockRunCleanupRules).toHaveBeenCalledTimes(2);
expect(mockRunCleanupRules).toHaveBeenCalledWith(service.dataSource, 38, [{ id: 1, userId: 38 }], false);
expect(mockRunCleanupRules).toHaveBeenCalledWith(service.dataSource, 39, [{ id: 2, userId: 39 }], false);
});

it('preview runs as a dry run and says nothing was deleted', async () => {
const { service } = makeService();
const rules = [{ id: 1, userId: 38 }];
jest.spyOn(service, 'getManyByIds').mockResolvedValue(rules as any);

const message = await service.doAction(makeReq('1', 'preview'), {});

expect(jobService.enqueue).toHaveBeenCalledTimes(2);
expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1] });
expect(jobService.enqueue).toHaveBeenCalledWith(39, 'attendance-cleanup-sweep', { ruleIds: [2] });
expect(mockRunCleanupRules).toHaveBeenCalledWith(service.dataSource, 38, rules, true);
expect(message).toContain('לא נמחק כלום');
});

it('falls through to the base doAction for unknown actions', async () => {
Expand Down
35 changes: 22 additions & 13 deletions server/src/entity-modules/attendance-cleanup-rule.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,48 @@ import { CrudAuthFilter } from '@shared/auth/crud-auth.filter';
import { BaseEntityService } from '@shared/base-entity/base-entity.service';
import { BaseEntityModuleOptions, Entity, InjectEntityRepository } from '@shared/base-entity/interface';
import { MailSendService } from '@shared/utils/mail/mail-send.service';
import { JobService } from '@shared/utils/jobs/job.service';
import { getAsNumberArray } from '@shared/utils/queryParam.util';
import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.entity';
import { runCleanupRules } from 'src/jobs/attendance-cleanup-sweep.handler';

class AttendanceCleanupRuleService<T extends Entity | AttendanceCleanupRule> extends BaseEntityService<T> {
constructor(
@InjectEntityRepository repo: Repository<T>,
mailSendService: MailSendService,
private readonly jobService: JobService,
) {
super(repo, mailSendService);
}

async doAction(req: CrudRequest<any, any>, body: any): Promise<any> {
const extra = req.parsed.extra;
switch (extra.action) {
case 'runNow': {
case 'runNow':
case 'preview': {
const ruleIds = getAsNumberArray(extra.ids) ?? [];
// getManyByIds applies crudAuth's filter (already merged into req.parsed.search
// by CrudRequestInterceptor) - a non-owned id in the selection simply doesn't
// come back, instead of being trusted from the request.
const rules = (await this.getManyByIds(req, ruleIds)) as unknown as AttendanceCleanupRule[];
// Admin sees everyone, so a selection can still span multiple owners - scope
// each enqueued job to its own rule subset.
const dryRun = extra.action === 'preview';
// Admin sees everyone, so a selection can still span multiple owners - run
// each owner's rules under its own userId.
const userIds = [...new Set(rules.map((rule) => rule.userId))];
const jobs = await Promise.all(
userIds.map((userId) =>
this.jobService.enqueue(userId, 'attendance-cleanup-sweep', {
ruleIds: rules.filter((rule) => rule.userId === userId).map((rule) => rule.id),
}),
),
);
return `נוצרו ${jobs.length} משימות ניקוי נוכחות (מזהים: ${jobs.map((job) => job.id).join(', ')})`;
let totalDeleted = 0;
const perRule: string[] = [];
for (const userId of userIds) {
const result = await runCleanupRules(
this.dataSource,
userId,
rules.filter((rule) => rule.userId === userId),
dryRun,
);
totalDeleted += result.totalDeleted;
perRule.push(...result.perRule);
}
const header = dryRun
? `תצוגה מקדימה, לא נמחק כלום. יימחקו ${totalDeleted} רשומות נוכחות`
: `נמחקו ${totalDeleted} רשומות נוכחות`;
return [header, ...perRule].join('\n');
}
}
return super.doAction(req, body);
Expand Down
35 changes: 32 additions & 3 deletions server/src/jobs/__tests__/attendance-cleanup-sweep.handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { In, Not } from 'typeorm';
import { AttendanceCleanupSweepHandler, getIsraelToday, getTargetDates } from '../attendance-cleanup-sweep.handler';
import { AttendanceCleanupSweepHandler, getIsraelToday, getTargetDates, runCleanupRules } from '../attendance-cleanup-sweep.handler';
import { Job } from '@shared/entities/Job.entity';

describe('getIsraelToday', () => {
Expand Down Expand Up @@ -43,11 +43,11 @@ describe('getTargetDates', () => {
describe('AttendanceCleanupSweepHandler', () => {
function makeDataSource(overrides: Partial<Record<string, any>> = {}) {
const repos: Record<string, any> = {
AttendanceCleanupRule: { find: jest.fn().mockResolvedValue([]) },
AttendanceCleanupRule: { find: jest.fn().mockResolvedValue([]), update: jest.fn() },
Lesson: { findOne: jest.fn() },
Klass: { findOne: jest.fn() },
StudentKlass: { find: jest.fn().mockResolvedValue([]) },
AttReport: { delete: jest.fn().mockResolvedValue({ affected: 0 }) },
AttReport: { delete: jest.fn().mockResolvedValue({ affected: 0 }), find: jest.fn().mockResolvedValue([]) },
...overrides,
};
return {
Expand Down Expand Up @@ -76,6 +76,7 @@ describe('AttendanceCleanupSweepHandler', () => {
it('deletes att_reports for students NOT recorded as members of the preserved klass', async () => {
const dataSource = makeDataSource({
AttendanceCleanupRule: {
update: jest.fn(),
find: jest.fn().mockResolvedValue([
{ id: 1, userId: 1, name: 'תפילה', lessonId: 5, klassId: 3, dayOfWeek: 4, weeksBack: 2, active: true },
]),
Expand All @@ -100,11 +101,38 @@ describe('AttendanceCleanupSweepHandler', () => {
expect(firstCallArgs.studentReferenceId).toEqual(Not(In([201, 202])));
expect(result.totalDeleted).toBe(14); // 7 + 7
expect(result.rulesProcessed).toBe(1);
expect(dataSource.repos.AttendanceCleanupRule.update).toHaveBeenCalledWith(1, {
lastRunAt: expect.any(Date),
lastRunSummary: 'נמחקו 14',
});
});

it('dry run lists the students that would be deleted, without deleting or saving a last run', async () => {
const rule = { id: 1, userId: 1, name: 'תפילה', lessonId: 5, klassId: 3, dayOfWeek: 4, weeksBack: 1, active: true };
const dataSource = makeDataSource({
Lesson: { findOne: jest.fn().mockResolvedValue({ id: 105 }) },
Klass: { findOne: jest.fn().mockResolvedValue({ id: 103 }) },
StudentKlass: { find: jest.fn().mockResolvedValue([{ studentReferenceId: 201 }]) },
AttReport: {
delete: jest.fn(),
find: jest.fn().mockResolvedValue([{ student: { name: 'רחל' } }, { student: null, studentTz: '123' }]),
},
});

const result = await runCleanupRules(dataSource, 1, [rule as any], true);

expect(dataSource.repos.AttReport.delete).not.toHaveBeenCalled();
expect(dataSource.repos.AttendanceCleanupRule.update).not.toHaveBeenCalled();
expect(dataSource.repos.AttReport.find.mock.calls[0][0].where.studentReferenceId).toEqual(Not(In([201])));
expect(result.totalDeleted).toBe(2);
expect(result.perRule[0]).toContain('יימחקו 2');
expect(result.perRule[0]).toContain('רחל, 123');
});

it('skips a date when the lesson or klass cannot be resolved for that year, without deleting', async () => {
const dataSource = makeDataSource({
AttendanceCleanupRule: {
update: jest.fn(),
find: jest.fn().mockResolvedValue([
{ id: 2, userId: 1, name: null, lessonId: 5, klassId: 3, dayOfWeek: 4, weeksBack: 1, active: true },
]),
Expand All @@ -124,6 +152,7 @@ describe('AttendanceCleanupSweepHandler', () => {
it('skips a date when no student is recorded as a member of the preserved klass, rather than deleting for everyone', async () => {
const dataSource = makeDataSource({
AttendanceCleanupRule: {
update: jest.fn(),
find: jest.fn().mockResolvedValue([
{ id: 3, userId: 1, name: 'x', lessonId: 5, klassId: 3, dayOfWeek: 4, weeksBack: 1, active: true },
]),
Expand Down
Loading
Loading