page-delete-config.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import type {
  2. IPageDeleteConfigValue,
  3. IPageDeleteConfigValueToProcessValidation,
  4. } from '~/interfaces/page-delete-config';
  5. import { PageDeleteConfigValue as Value } from '~/interfaces/page-delete-config';
  6. /**
  7. * Return true if "configForRecursive" is stronger than "configForSingle"
  8. * Strength: "Admin" > "Admin and author" > "Anyone"
  9. * @param configForSingle IPageDeleteConfigValueToProcessValidation
  10. * @param configForRecursive IPageDeleteConfigValueToProcessValidation
  11. * @returns boolean
  12. */
  13. export const validateDeleteConfigs = (
  14. configForSingle: IPageDeleteConfigValueToProcessValidation,
  15. configForRecursive: IPageDeleteConfigValueToProcessValidation,
  16. ): boolean => {
  17. if (configForSingle === Value.Anyone) {
  18. switch (configForRecursive) {
  19. case Value.Anyone:
  20. case Value.AdminAndAuthor:
  21. case Value.AdminOnly:
  22. return true;
  23. }
  24. }
  25. if (configForSingle === Value.AdminAndAuthor) {
  26. switch (configForRecursive) {
  27. case Value.Anyone:
  28. return false;
  29. case Value.AdminAndAuthor:
  30. case Value.AdminOnly:
  31. return true;
  32. }
  33. }
  34. if (configForSingle === Value.AdminOnly) {
  35. switch (configForRecursive) {
  36. case Value.Anyone:
  37. case Value.AdminAndAuthor:
  38. return false;
  39. case Value.AdminOnly:
  40. return true;
  41. }
  42. }
  43. return false;
  44. };
  45. /**
  46. * Convert IPageDeleteConfigValue.Inherit to the calculable value
  47. * @param confForSingle IPageDeleteConfigValueToProcessValidation
  48. * @param confForRecursive IPageDeleteConfigValue
  49. * @returns [(value for single), (value for recursive)]
  50. */
  51. export const prepareDeleteConfigValuesForCalc = (
  52. confForSingle: IPageDeleteConfigValueToProcessValidation | undefined,
  53. confForRecursive: IPageDeleteConfigValue | undefined,
  54. ): [
  55. IPageDeleteConfigValueToProcessValidation,
  56. IPageDeleteConfigValueToProcessValidation,
  57. ] => {
  58. // convert undefined to default values
  59. const confForSingleToReturn = confForSingle ?? Value.Anyone;
  60. const confForRecursiveToReturn = confForRecursive ?? Value.Inherit;
  61. if (confForRecursiveToReturn === Value.Inherit) {
  62. return [confForSingleToReturn, confForSingleToReturn];
  63. }
  64. return [confForSingleToReturn, confForRecursiveToReturn];
  65. };