|
1 | | -You are an expert in TypeScript, Angular, and scalable web application development. You write maintainable, performant, and accessible code following Angular and TypeScript best practices. |
2 | | - |
3 | | -## TypeScript Best Practices |
4 | | - |
5 | | -- Use strict type checking |
6 | | -- Prefer type inference when the type is obvious |
7 | | -- Avoid the `any` type; use `unknown` when type is uncertain |
8 | | - |
9 | | -## Angular Best Practices |
10 | | - |
11 | | -- Always use standalone components over NgModules |
12 | | -- Must NOT set `standalone: true` inside Angular decorators. It's the default. |
13 | | -- Use signals for state management |
14 | | -- Implement lazy loading for feature routes |
15 | | -- Do NOT use the `@HostBinding` and `@HostListener` decorators. Put host bindings inside the `host` object of the `@Component` or `@Directive` decorator instead |
16 | | -- Use `NgOptimizedImage` for all static images. |
17 | | - - `NgOptimizedImage` does not work for inline base64 images. |
18 | | - |
19 | | -## Components |
20 | | - |
21 | | -- Keep components small and focused on a single responsibility |
22 | | -- Use `input()` and `output()` functions instead of decorators |
23 | | -- Use `computed()` for derived state |
24 | | -- Set `changeDetection: ChangeDetectionStrategy.OnPush` in `@Component` decorator |
25 | | -- Prefer inline templates for small components |
26 | | -- Prefer Reactive forms instead of Template-driven ones |
27 | | -- Do NOT use `ngClass`, use `class` bindings instead |
28 | | -- Do NOT use `ngStyle`, use `style` bindings instead |
29 | | -- Use Angular Material components, avoid custom UI components when possible |
30 | | -- Avoid CSS when a custom component is provided by Angular Material or us (e.g., app-flex) |
31 | | - |
32 | | -## State Management |
33 | | - |
34 | | -- Use signals for local component state |
35 | | -- Use `computed()` for derived state |
36 | | -- Keep state transformations pure and predictable |
37 | | -- Do NOT use `mutate` on signals, use `update` or `set` instead |
38 | | - |
39 | | -## Templates |
40 | | - |
41 | | -- Keep templates simple and avoid complex logic |
42 | | -- Use native control flow (`@if`, `@for`, `@switch`) instead of `*ngIf`, `*ngFor`, `*ngSwitch` |
43 | | -- Use the async pipe to handle observables |
44 | | - |
45 | | -## Services |
46 | | - |
47 | | -- Design services around a single responsibility |
48 | | -- Use the `providedIn: 'root'` option for singleton services |
49 | | -- Use the `inject()` function instead of constructor injection |
50 | | - |
51 | | -## Styling |
52 | | -- Use Angular's built-in styling capabilities (e.g., component styles) |
53 | | -- Prefer Angular Material CSS variables for theming |
54 | | -- Avoid global styles; scope styles to components |
| 1 | +You are GitHub Copilot working in the BankTracker GraphQL monorepo. Follow the patterns already in the codebase before introducing new approaches. |
| 2 | + |
| 3 | +Keep in mind the basics of programming like |
| 4 | + |
| 5 | +YAGNI, DRY2, NEVER RESORT TO USE SOLID |
| 6 | + |
| 7 | +## Architecture |
| 8 | +- GraphQL backend (`PhantomDave.BankTracking.Api`) exposes accounts and finance records via HotChocolate; schema is composed from `Types/Queries|Mutations|Inputs|ObjectTypes` with `ExtendObjectType` partials. |
| 9 | +- Data layer (`PhantomDave.BankTracking.Data`) wraps EF Core against PostgreSQL with a repository + unit-of-work abstraction registered through `AddDataAccess`. |
| 10 | +- Domain models live in `PhantomDave.BankTracking.Library` and are shared by API, data, and background jobs. |
| 11 | +- Angular 20 frontend (`frontend/`) uses Apollo Angular with generated GQL services, Angular Material, and signals; state is mostly managed inside `models/*-service.ts` files. |
| 12 | +- A hosted service (`RecurringFinanceRecordService`) materializes recurring finance records by cloning templates on a timed loop, so new recurrence features must remain idempotent. |
| 13 | + |
| 14 | +## Backend (ASP.NET + HotChocolate) |
| 15 | +- Prefer throwing `GraphQLException` built via `ErrorBuilder` with explicit `SetCode` values; clients rely on codes like `BAD_USER_INPUT`, `UNAUTHENTICATED`, and `NOT_FOUND` for UX. |
| 16 | +- Retrieve the authenticated account id by calling `httpContextAccessor.GetAccountIdFromContext()`; the JWT must contain `ClaimTypes.NameIdentifier` or mutations will fail. |
| 17 | +- Keep service logic inside `Services/*Service.cs` and reuse the injected `IUnitOfWork`; call `SaveChangesAsync` on the unit of work to commit changes. |
| 18 | +- Normalize and validate inputs (e.g., currency via `NormalizeCurrency`, description length trimming) before persistence to keep recurring job assumptions intact. |
| 19 | +- New GraphQL types should extend the root via `[ExtendObjectType(OperationTypeNames.Query|Mutation)]` and convert entities using `From*` factory helpers in `Types/ObjectTypes`. |
| 20 | +- Database migrations live in `PhantomDave.BankTracking.Data/Migrations`; ensure Postgres is running (`docker compose -f compose.dev.yaml up -d database`) before applying or updating schema. |
| 21 | + |
| 22 | +## Frontend (Angular + Apollo) |
| 23 | +- App uses `provideZonelessChangeDetection` and signals; prefer `signal()`, `computed()`, and `effect()` over RxJS state unless bridging to Apollo streams. |
| 24 | +- Components are standalone by default—do not add `standalone: true` manually—but always list dependencies in `imports` and lean on Angular Material before custom UI. |
| 25 | +- Use the generated Apollo classes (e.g., `CreateAccountGQL`, `GetFinanceRecordsGQL`) with `firstValueFrom` or `.watch().valueChanges`; match operation names with `refetchQueries` strings (`'getFinanceRecords'`). |
| 26 | +- Local auth state is stored in `localStorage` as `sessionData`; guards and the `unauthorizedInterceptor` expect `SessionData` to stay in sync with backend `verifyToken` responses. |
| 27 | +- Prefer signals over component-level Observables; when you must bridge, update signals in a `tap` and remember to set/reset `_loading` and `_error` signals like existing services do. |
| 28 | +- UI feedback goes through `SnackbarService`; reuse `snackbar.success/error` rather than opening `MatSnackBar` manually for consistent styling. |
| 29 | + |
| 30 | +## GraphQL Workflow |
| 31 | +- Add operations as `.graphql` files near the feature (see `frontend/src/app/models/finance-record/gql/*.graphql`); keep operation names unique and PascalCase or camelCase that matches existing usage. |
| 32 | +- Run `npm run codegen` (auto-runs on `npm start` via the `prestart` script) to refresh `src/generated/graphql.ts` and `schema.graphql`; the script reads the backend JWT secret from `appsettings.Development.json` or environment variables. |
| 33 | +- If the backend schema changes, run `./update-schema.sh` after starting the API (`cd PhantomDave.BankTracking.Api && dotnet run`) so the codegen script can reach `http://localhost:5095/graphql`. |
| 34 | +- When adding scalar fields, update the `scalars` mapping in `frontend/codegen.ts` only if the backend type is new; clients currently assume `DateTime` → `string` converted to `Date` in services. |
| 35 | + |
| 36 | +## Developer Workflow |
| 37 | +- Backend: `dotnet run` (or VS Code task `build`) from `PhantomDave.BankTracking.Api`; the app auto-migrates the database on startup. |
| 38 | +- Frontend: `npm run start` in `frontend/` (triggers codegen). Fix the failing command first if schema generation fails—usually missing backend or JWT secret. |
| 39 | +- Tests: there are no automated tests yet; verify critical flows manually (login, recurring creation) after impactful changes. |
| 40 | +- Docker: the root `Dockerfile` builds the API only; run `docker compose -f compose.dev.yaml up -d database` for a local Postgres instance. |
| 41 | + |
| 42 | +## Conventions & Pitfalls |
| 43 | +- Stick to ASCII in code and messages; existing Italian copy in snackbars is unintentional, translate them to english. |
| 44 | +- Avoid `@HostBinding`/`@HostListener`; register host styles via the `host` object (see `FlexComponent`). |
| 45 | +- Signals should not call `.mutate`; use `.set`/`.update` as in `AccountService` and `FinanceRecordService` to keep change detection predictable without zones. |
| 46 | +- Background service creates non-recurring instances by copying template records; new fields must populate both the recurring template and cloned instance to avoid drift. |
| 47 | +- Currency values are persisted as `numeric(18,2)`; always send numbers (not strings) from the frontend and uppercase 3-letter codes to satisfy backend validation. |
| 48 | +- Keep GraphQL error codes stable—UI interceptors look specifically for `UNAUTHENTICATED` to trigger logout and route redirects. |
0 commit comments