fix: Split admin comm service into multiple services

This commit is contained in:
2025-06-11 14:11:14 +02:00
parent 7fedaf09dc
commit 5a6f036cb7
62 changed files with 816 additions and 669 deletions

View File

@@ -0,0 +1,33 @@
<h1 mat-dialog-title>Tworzenie wpisów do jadłospisu</h1>
<mat-dialog-content>
<mat-radio-group [(ngModel)]="type">
<mat-radio-button value="day">Dzień</mat-radio-button>
<mat-radio-button value="week">Tydzień</mat-radio-button>
<mat-radio-button value="file">Plik</mat-radio-button>
</mat-radio-group>
<div>
@switch (type) {
@case ("day") {
<app-date-selector [filter]="filter" [(date)]="day"></app-date-selector>
}
@case ("week") {
<mat-form-field>
<mat-label>Wybierz tydzień</mat-label>
<mat-date-range-input [rangePicker]="picker" [formGroup]="range">
<input matStartDate formControlName="start">
<input matEndDate formControlName="end">
</mat-date-range-input>
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
}
@case ("file") {
<button mat-flat-button color="accent" (click)="activateUpload()">Otwórz okno</button>
}
}
</div>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-raised-button color="accent" (click)="submit()">Utwórz pozycje</button>
<button mat-button mat-dialog-close>Anuluj</button>
</mat-dialog-actions>

View File

@@ -0,0 +1,39 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { MenuAddComponent } from './menu-add.component'
import {
MAT_DIALOG_DATA,
MatDialogModule,
MatDialogRef,
} from '@angular/material/dialog'
import { MatRadioModule } from '@angular/material/radio'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
describe('MenuAddComponent', () => {
let component: MenuAddComponent
let fixture: ComponentFixture<MenuAddComponent>
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MenuAddComponent],
providers: [
{ provide: MAT_DIALOG_DATA, useValue: {} },
{ provide: MatDialogRef, useValue: {} },
],
imports: [
MatDialogModule,
MatRadioModule,
ReactiveFormsModule,
FormsModule,
],
}).compileComponents()
fixture = TestBed.createComponent(MenuAddComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should create', () => {
expect(component).toBeTruthy()
})
})

View File

@@ -0,0 +1,60 @@
import { Component } from '@angular/core'
import { MenuUploadComponent } from '../menu-upload/menu-upload.component'
import { MatDialog, MatDialogRef } from '@angular/material/dialog'
import { FDSelection, weekendFilter } from 'src/app/fd.da'
import { FormControl, FormGroup } from '@angular/forms'
import { MAT_DATE_RANGE_SELECTION_STRATEGY } from '@angular/material/datepicker'
import { DateTime } from 'luxon'
@Component({
selector: 'app-menu-add',
templateUrl: './menu-add.component.html',
styleUrl: './menu-add.component.scss',
providers: [
{ provide: MAT_DATE_RANGE_SELECTION_STRATEGY, useClass: FDSelection },
],
standalone: false,
})
export class MenuAddComponent {
type: string | undefined
filter = weekendFilter
day: string = DateTime.now().toISODate()
range = new FormGroup({
start: new FormControl<DateTime | null>(null),
end: new FormControl<DateTime | null>(null),
})
constructor(
public dialogRef: MatDialogRef<MenuAddComponent>,
private dialog: MatDialog
) {}
submit() {
switch (this.type) {
case 'day':
this.dialogRef.close({ type: 'day', value: this.day })
break
case 'week':
this.dialogRef.close({
type: 'week',
value: { start: this.range.value.start?.toISODate(), count: 5 },
})
break
default:
break
}
}
activateUpload() {
this.dialog
.open(MenuUploadComponent)
.afterClosed()
.subscribe(data => {
if (data) {
this.dialogRef.close({ type: 'file', ...data })
}
})
}
}

View File

@@ -0,0 +1,141 @@
<div id="upper-bar">
<mat-form-field>
<mat-label>Wybierz tydzień</mat-label>
<mat-date-range-input [rangePicker]="picker" [formGroup]="range">
<input matStartDate formControlName="start" (dateChange)="requestData()">
<input matEndDate formControlName="end" (dateChange)="requestData()">
</mat-date-range-input>
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
<button mat-icon-button (click)="requestData()"><mat-icon>refresh</mat-icon></button>
<button mat-icon-button (click)="addDate()"><mat-icon>add</mat-icon></button>
<button mat-icon-button (click)="print()"><mat-icon>print</mat-icon></button>
</div>
@if (loading) {
<mat-spinner></mat-spinner>
}
<table mat-table [dataSource]="dataSource">
<div matColumnDef="day">
<th mat-header-cell *matHeaderCellDef>Dzień</th>
<td mat-cell *matCellDef="let element">
<span>{{element.day.toFormat('dd.LL.yyyy')}}r.</span>
<p>{{element.day.toFormat('cccc') | titlecase }}</p>
<app-field-editor category="Nazwa" [(word)]="element.dayTitle" (wordChange)="editTitle(element._id)"/><br><hr>
<button (click)="remove(element._id)">Usuń dzień</button>
</td>
</div>
<div matColumnDef="sn">
<th mat-header-cell *matHeaderCellDef>Śniadanie</th>
<td mat-cell *matCellDef="let element">
<ul class="non-editable">
@for (i of ls.defaultItems.sn; track i) {
<li>{{i}}</li>
}
</ul><hr>
<app-list-editor [(list)]="element.sn.fancy" (edit)="editSn(element._id)" dataList="sn-fancy"/><hr>
<ul>
<li><app-field-editor category="II Śniadanie" [(word)]="element.sn.second" list="sn-second" (wordChange)="editSn(element._id)"/></li>
</ul>
</td>
</div>
<div matColumnDef="ob">
<th mat-header-cell *matHeaderCellDef>Obiad</th>
<td mat-cell *matCellDef="let element">
<ul>
<li><app-field-editor category="Zupa" [(word)]="element.ob.soup" list="ob-soup" (wordChange)="editOb(element._id)"/></li>
<li><app-field-editor category="Vege" [(word)]="element.ob.vege" list="ob-vege" (wordChange)="editOb(element._id)"/></li>
<li><app-field-editor category="Danie główne" [(word)]="element.ob.meal" list="ob-meal" (wordChange)="editOb(element._id)"/></li>
</ul><hr>
<app-list-editor [(list)]="element.ob.condiments" (edit)="editOb(element._id)" dataList="ob-condiments"/><hr>
<ul>
<li><app-field-editor category="Napój" [(word)]="element.ob.drink" list="ob-drink" (wordChange)="editOb(element._id)"/></li>
</ul><hr>
<app-list-editor [(list)]="element.ob.other" (edit)="editOb(element._id)" dataList="ob-other"/>
<button (click)="getStat(element.day, 'ob')">
Opinie wychowanków
</button>
</td>
</div>
<div matColumnDef="kol">
<th mat-header-cell *matHeaderCellDef>Kolacja</th>
<td mat-cell *matCellDef="let element">
<div>
@switch (element.day.weekday) {
@default {
<div>
<ul class="non-editable">
@for (i of ls.defaultItems.kol; track i) {
<li>{{i}}</li>
}
</ul><hr>
<ul>
<li><app-field-editor category="Kolacja" [(word)]="element.kol" list="kol" (wordChange)="editKol(element._id)"/></li>
</ul>
<button (click)="getStat(element.day, 'kol')">
Opinie wychowanków
</button>
</div>
}
@case (5) {
<div class="non-editable">
<p>Kolacja w domu!</p>
<p>(Nie edytowalne)</p>
</div>
}
}
</div>
</td>
</div>
<tr mat-header-row *matHeaderRowDef="dcols"></tr>
<tr mat-row *matRowDef="let row; columns: dcols"></tr>
</table>
@if (options) {
<datalist id="sn-fancy">
@for (i of options.sn.fancy; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="sn-second">
@for (i of options.sn.second; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="ob-soup">
@for (i of options.ob.soup; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="ob-vege">
@for (i of options.ob.vege; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="ob-meal">
@for (i of options.ob.meal; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="ob-condiments">
@for (i of options.ob.condiments; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="ob-drink">
@for (i of options.ob.drink; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="ob-other">
@for (i of options.ob.other; track i) {
<option>{{i}}</option>
}
</datalist>
<datalist id="kol">
@for (i of options.kol; track i) {
<option>{{i}}</option>
}
</datalist>
}

View File

@@ -0,0 +1,18 @@
#upper-bar {
display: flex;
}
mat-form-field {
flex-grow: 1;
}
button[mat-icon-button] {
margin-left: 4pt;
margin-right: 4pt;
margin-top: 4pt;
}
.non-editable {
color: gray;
font-style: italic;
}

View File

@@ -0,0 +1,51 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { MenuEditComponent } from './menu-edit.component'
import { MatTableModule } from '@angular/material/table'
import { MatInputModule } from '@angular/material/input'
import {
MAT_DATE_RANGE_SELECTION_STRATEGY,
MatDatepickerModule,
} from '@angular/material/datepicker'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
import { FDSelection } from 'src/app/fd.da'
import { ReactiveFormsModule } from '@angular/forms'
import { of } from 'rxjs'
import { MatDialogModule } from '@angular/material/dialog'
import { MatIconModule } from '@angular/material/icon'
import { provideLuxonDateAdapter } from '@angular/material-luxon-adapter'
xdescribe('MenuEditComponent', () => {
let component: MenuEditComponent
let fixture: ComponentFixture<MenuEditComponent>
beforeEach(() => {
const acMock = jasmine.createSpyObj('AdminCommService', {
getMenu: of(),
})
TestBed.configureTestingModule({
declarations: [MenuEditComponent],
imports: [
MatTableModule,
MatInputModule,
MatDatepickerModule,
BrowserAnimationsModule,
ReactiveFormsModule,
MatDialogModule,
MatIconModule,
],
providers: [
provideLuxonDateAdapter(),
{ provide: MAT_DATE_RANGE_SELECTION_STRATEGY, useClass: FDSelection },
// { provide: AdminCommService, useValue: acMock },
],
})
fixture = TestBed.createComponent(MenuEditComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should create', () => {
expect(component).toBeTruthy()
})
})

View File

@@ -0,0 +1,161 @@
import { Component } from '@angular/core'
import { FormControl, FormGroup } from '@angular/forms'
import { MAT_DATE_RANGE_SELECTION_STRATEGY } from '@angular/material/datepicker'
import { FDSelection } from 'src/app/fd.da'
import { Menu } from 'src/app/types/menu'
import { MatTableDataSource } from '@angular/material/table'
import { MatDialog } from '@angular/material/dialog'
import { MenuUploadComponent } from './menu-upload/menu-upload.component'
import { Status } from 'src/app/types/status'
import { MatSnackBar } from '@angular/material/snack-bar'
import { MenuAddComponent } from './menu-add/menu-add.component'
import { LocalStorageService } from 'src/app/services/local-storage.service'
import { DateTime } from 'luxon'
import { MenuEditService } from './menu-edit.service'
@Component({
selector: 'app-menu-edit',
templateUrl: './menu-edit.component.html',
styleUrls: ['./menu-edit.component.scss'],
providers: [
{ provide: MAT_DATE_RANGE_SELECTION_STRATEGY, useClass: FDSelection },
],
standalone: false,
})
export class MenuEditComponent {
dcols: string[] = ['day', 'sn', 'ob', 'kol']
dataSource: MatTableDataSource<Menu> = new MatTableDataSource<Menu>()
range = new FormGroup({
start: new FormControl<DateTime | null>(null),
end: new FormControl<DateTime | null>(null),
})
loading = false
public options: any
constructor(
private ac: MenuEditService,
private dialog: MatDialog,
private sb: MatSnackBar,
readonly ls: LocalStorageService
) {}
print() {
this.ac
.print(this.range.value.start, this.range.value.end)
?.subscribe(r => {
if (r && r.length > 0) {
var mywindow = window.open(
undefined,
'Drukowanie',
'height=400,width=400'
)
mywindow?.document.write(r)
mywindow?.print()
mywindow?.close()
}
})
}
addDate() {
this.dialog
.open(MenuAddComponent)
.afterClosed()
.subscribe(data => {
if (data) {
switch (data.type) {
case 'day':
this.ac.new
.single(data.value)
.subscribe(s => this.refreshIfGood(s))
break
case 'week':
this.ac.new
.range(data.value.start, data.value.count)
.subscribe(s => this.refreshIfGood(s))
break
case 'file':
this.requestData()
break
default:
break
}
}
})
}
requestData() {
this.loading = true
this.ac.getOpts().subscribe(o => {
this.options = o
})
this.ac
.getMenu(this.range.value.start, this.range.value.end)
?.subscribe(data => {
this.loading = false
this.dataSource.data = data.map(v => {
let newMenu: Menu = {
...v,
day: DateTime.fromISO(v.day),
}
return newMenu
})
})
}
private refreshIfGood(s: Status) {
if (s.status.toString().match(/2\d\d/)) {
this.requestData()
}
}
activateUpload() {
this.dialog
.open(MenuUploadComponent)
.afterClosed()
.subscribe(data => {
if (data) {
this.requestData()
}
})
}
editSn(id: string) {
this.ac
.editSn(id, this.dataSource.data.find(v => v._id == id)!.sn)
.subscribe(s => this.refreshIfGood(s))
}
editOb(id: string) {
this.ac
.editOb(id, this.dataSource.data.find(v => v._id == id)!.ob)
.subscribe(s => this.refreshIfGood(s))
}
editKol(id: string) {
this.ac
.editKol(id, this.dataSource.data.find(v => v._id == id)?.kol)
.subscribe(s => this.refreshIfGood(s))
}
editTitle(id: string) {
this.ac
.editTitle(id, this.dataSource.data.find(v => v._id == id)?.dayTitle)
.subscribe(s => this.refreshIfGood(s))
}
getStat(day: DateTime, m: 'ob' | 'kol') {
this.ac
.stat(day, m)
.subscribe(s =>
this.sb.open(
`${s.y} / ${s.y + s.n} = ${((s.y / (s.y + s.n)) * 100).toFixed(2)}%`,
'Zamknij',
{ duration: 2500 }
)
)
}
remove(id: string) {
this.ac.rm(id).subscribe(s => this.refreshIfGood(s))
}
}

View File

@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MenuEditService } from './menu-edit.service';
describe('MenuEditService', () => {
let service: MenuEditService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MenuEditService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@@ -0,0 +1,110 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { DateTime } from 'luxon';
import { Menu } from 'src/app/types/menu';
import { Status } from 'src/app/types/status';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class MenuEditService {
constructor(private http: HttpClient) { }
getMenu(start?: DateTime | null, end?: DateTime | null) {
if (start && end) {
const body = { start: start.toString(), end: end.toString() }
return this.http.get<(Omit<Menu, 'day'> & { day: string })[]>(
environment.apiEndpoint + '/admin/menu',
{ withCredentials: true, params: body }
)
}
return
}
getOpts() {
return this.http.get<any>(environment.apiEndpoint + `/admin/menu/opts`, {
withCredentials: true,
})
}
postMenu(file: File) {
if (file) {
const formData = new FormData()
formData.append('menu', file)
return this.http.post<Status>(
environment.apiEndpoint + '/admin/menu/upload',
formData,
{ withCredentials: true }
)
}
return
}
editSn(id: string, content: Menu['sn']) {
return this.putMenu(id, { sn: content })
}
editOb(id: string, content: Menu['ob']) {
return this.putMenu(id, { ob: content })
}
editKol(id: string, content: Menu['kol']) {
return this.putMenu(id, { kol: content })
}
editTitle(id: string, content: Menu['dayTitle']) {
return this.putMenu(id, { dayTitle: content })
}
print(start?: DateTime | null, end?: DateTime | null) {
if (start && end) {
const body = { start: start.toString(), end: end.toString() }
return this.http.get(environment.apiEndpoint + '/admin/menu/print', {
withCredentials: true,
params: body,
responseType: 'text',
})
}
return
}
stat(day: DateTime, m: 'ob' | 'kol') {
return this.http.get<{ y: number; n: number }>(
environment.apiEndpoint + `/admin/menu/${day.toISO()}/votes/${m}`,
{ withCredentials: true }
)
}
new = {
single: (day: DateTime) => {
return this.http.post<Status>(
environment.apiEndpoint + `/admin/menu/${day.toISO()}`,
null,
{ withCredentials: true }
)
},
range: (start: DateTime, count: number) => {
return this.http.post<Status>(
environment.apiEndpoint + `/admin/menu/${start.toISO()}/${count}/`,
null,
{ withCredentials: true }
)
},
}
rm(id: string) {
return this.http.delete<Status>(
environment.apiEndpoint + `/admin/menu/${id}`,
{ withCredentials: true }
)
}
private putMenu(id: string, update: Partial<Menu>) {
return this.http.put<Status>(
environment.apiEndpoint + `/admin/menu/${id}`,
update,
{ withCredentials: true }
)
}
}

View File

@@ -0,0 +1,20 @@
<h1 mat-dialog-title>Import z pliku</h1>
<mat-dialog-content>
<input type="file" name="menu" #fu style="display: none;" (change)="onFileChange($event)" accept=".xlsx,.ods,application/vnd.oasis.opendocument.spreadsheet">
<button mat-raised-button color="accent" (click)="fu.click()">Wybierz plik</button>
<div style="color: red;">
<h1>UWAGA!</h1>
<h3>Przed wysłaniem upewnij się że</h3>
<ul>
<li>Daty w pliku są prawidłowe i poprawnie sformatowane (DD.MM.RRRR)</li>
<li>Wszystkie pozycje w menu są w osobnych linijkach</li>
<li>Załączony dokument to arkusz w formacie XLSX lub ODS</li>
</ul>
<h2>Nie spełnienie któregokolwiek z tych wymagań może skutkować szkodami w programie!</h2>
<h3>Późniejsza modyfikacja danych jest niemożliwa w tej wersji programu.</h3>
</div>
</mat-dialog-content>
<mat-dialog-actions>
<button mat-raised-button color="warn" [disabled]="file == undefined" (click)="submit()">Wyślij</button>
<button mat-button mat-dialog-close>Anuluj</button>
</mat-dialog-actions>

View File

@@ -0,0 +1,4 @@
:host {
margin: 8pt;
display: block;
}

View File

@@ -0,0 +1,28 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { MenuUploadComponent } from './menu-upload.component'
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog'
xdescribe('MenuUploadComponent', () => {
let component: MenuUploadComponent
let fixture: ComponentFixture<MenuUploadComponent>
beforeEach(() => {
const acMock = jasmine.createSpyObj('AdminCommService', ['postMenu'])
TestBed.configureTestingModule({
declarations: [MenuUploadComponent],
providers: [
// { provide: AdminCommService, useValue: acMock },
{ provide: MatDialogRef, useValue: {} },
],
imports: [MatDialogModule],
})
fixture = TestBed.createComponent(MenuUploadComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should create', () => {
expect(component).toBeTruthy()
})
})

View File

@@ -0,0 +1,31 @@
import { Component } from '@angular/core'
import { MatDialogRef } from '@angular/material/dialog'
import { MenuEditService } from '../menu-edit.service'
@Component({
selector: 'app-upload-edit',
templateUrl: './menu-upload.component.html',
styleUrls: ['./menu-upload.component.scss'],
standalone: false,
})
export class MenuUploadComponent {
constructor(
private ac: MenuEditService,
public dialogRef: MatDialogRef<MenuUploadComponent>
) {}
protected file: File | undefined
onFileChange(event: Event) {
const file: File = (event.target as HTMLInputElement).files![0]
if (file) {
this.file = file
} else {
this.file = undefined
}
}
submit() {
this.ac.postMenu(this.file!)?.subscribe(value => {
this.dialogRef.close(value)
})
}
}