reportItem.service.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Injectable } from '@angular/core';
  2. // import {bootstrapItem} from "../../../node_modules/@angular/cli/lib/ast-tools";
  3. export enum ReportItemType {
  4. food = 'Food',
  5. travel = 'Travel',
  6. training = 'Training',
  7. transport = 'Transport',
  8. unselected = ''
  9. }
  10. export type ReportItem = {
  11. description: string;
  12. amount: number;
  13. hasReceipt: boolean;
  14. type: ReportItemType;
  15. date: Date;
  16. };
  17. // A ReportItem with all keys optional.
  18. export type ReportItem2 = {
  19. // All properties same as ReportItem.
  20. // [P in keyof ReportItem]?: ReportItem[P]
  21. // All properties optional
  22. [P in keyof ReportItem]?: ReportItem[P]
  23. }
  24. // A Generic for any object with all keys optional.
  25. type Optional<T> = {
  26. // All properties optional
  27. [P in keyof T]?: T[P]
  28. }
  29. // Same as ReportItem2 but using the generic.
  30. export type ReportItem3 = Optional<ReportItem>;
  31. // Same again, using the TS builtin for this.
  32. export type ReportItem4 = Partial<ReportItem>;
  33. // All keys readonly.
  34. export type ReportItem5 = Readonly<ReportItem>;
  35. // ReportItem again.
  36. export type ReportItem6 = Required<ReportItem3>;
  37. // Only keep 3 keys
  38. export type ReportItem7 = Pick<ReportItem, 'date' | 'amount' | 'description' >;
  39. function validateRequired(item: ReportItem): boolean {
  40. // false is a valid value for these, so exclude them from check.
  41. const exclude: (keyof ReportItem)[] = ['hasReceipt'];
  42. return Object.keys(item)
  43. .reduce((isValid, memberKey: keyof ReportItem) => {
  44. if (!isValid) {
  45. return isValid;
  46. }
  47. if (memberKey === 'date') {
  48. // date is of type Date, because of the keyof typing.
  49. const date = item[memberKey];
  50. if (date < new Date(1970)) {
  51. return false;
  52. }
  53. }
  54. return exclude.includes(memberKey) || !!item[memberKey];
  55. }, true);
  56. }
  57. function validateFoodItem(item: ReportItem): string {
  58. if (item.amount >= 50 && !item.hasReceipt) {
  59. return 'A food item with a value greater than $50 must have a receipt';
  60. }
  61. return '';
  62. }
  63. function validateReceipt(item: ReportItem): string {
  64. return item.hasReceipt ? '' : 'The item must have a receipt';
  65. }
  66. function validateTraining(item: ReportItem): string {
  67. if (item.amount < 50 && !item.hasReceipt) {
  68. return '';
  69. }
  70. return 'A training item with a value greater than $50 must have a receipt';
  71. }
  72. const validateDate = (minDate: Date, maxDate: Date) => (item: ReportItem): string => {
  73. return +item.date >= +minDate && +item.date <= +maxDate ? '' : 'The date is invalid';
  74. };
  75. @Injectable()
  76. export class ReportItemService {
  77. private validateDate: (item: ReportItem) => string;
  78. constructor() {
  79. const today = new Date();
  80. const maxDate = new Date(today.getFullYear(), today.getMonth(), today.getDate());
  81. const minDate = new Date(maxDate);
  82. minDate.setMonth(-3);
  83. this.validateDate = validateDate(minDate, maxDate);
  84. }
  85. isValid(item: ReportItem): string {
  86. if (!validateRequired(item)) {
  87. return 'One of the required fields was not filled.';
  88. }
  89. switch (item.type) {
  90. case ReportItemType.food: {
  91. const validatedFood = validateFoodItem(item);
  92. if (validatedFood) {
  93. return validatedFood;
  94. }
  95. return this.validateDate(item);
  96. }
  97. case ReportItemType.training:
  98. return validateTraining(item);
  99. case ReportItemType.transport:
  100. case ReportItemType.travel: {
  101. const validatedReceipt = validateReceipt(item);
  102. if (validatedReceipt) {
  103. return validatedReceipt;
  104. }
  105. return this.validateDate(item);
  106. }
  107. case ReportItemType.unselected:
  108. default:
  109. return 'The item type is not supported';
  110. }
  111. }
  112. }