Advertisement
djbob2000

Untitled

Jul 29th, 2024
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // FOR_OF =========================
  2. async function modifyResourceFromDatabase(
  3.     resource: IWithId,
  4.     resourceMeta: ResourceMeta,
  5.     userId: string,
  6.     resourceFromDb: IWithId | null = null,
  7.     isCalledFromOutside: boolean = false
  8. ) {
  9.     const clrType = resourceTypeProvider.getResourceTypeByName(resourceMeta.resourceName);
  10.  
  11.     if (!resourceFromDb) {
  12.         resourceFromDb = await typeRepository.getOneById(clrType, resource.id);
  13.     }
  14.  
  15.     for (const propertyMeta of Object.values(resourceMeta.properties)) {
  16.         if (((propertyMeta.isOutsideEditable && isCalledFromOutside) || !isCalledFromOutside)
  17.             && propertyMeta.type !== PropertyType.Object
  18.             && propertyMeta.type !== PropertyType.Array) {
  19.            
  20.             const newPropertyValue = propertyMetaReflectionService.getPropertyValue(resource, propertyMeta);
  21.             const existingPropertyValue = propertyMetaReflectionService.getPropertyValue(resourceFromDb, propertyMeta);
  22.  
  23.             if ((newPropertyValue !== null && !isEqual(newPropertyValue, existingPropertyValue)) ||
  24.                 (existingPropertyValue !== null && !isEqual(existingPropertyValue, newPropertyValue))) {
  25.                 await permissionChecker.checkPropertyPermission(resourceMeta, CrudAction.Update, userId,
  26.                     resource, propertyMeta.id);
  27.             }
  28.  
  29.             propertyMetaReflectionService.setPropertyValueFromAnotherResource(resourceFromDb, resource, propertyMeta);
  30.         }
  31.     }
  32.  
  33.     if (editBaseResourceService.isBaseResource(clrType)) {
  34.         await editBaseResourceService.setModifiedProperties(resourceFromDb as IBaseResource, userId);
  35.     }
  36.  
  37.     return resourceFromDb;
  38. }
  39.  
  40.  
  41.  
  42. //forEach ===============================
  43. async function modifyResourceFromDatabase(
  44.     resource: IWithId,
  45.     resourceMeta: ResourceMeta,
  46.     userId: string,
  47.     resourceFromDb: IWithId | null = null,
  48.     isCalledFromOutside: boolean = false
  49. ) {
  50.     const clrType = resourceTypeProvider.getResourceTypeByName(resourceMeta.resourceName);
  51.  
  52.     if (!resourceFromDb) {
  53.         resourceFromDb = await typeRepository.getOneById(clrType, resource.id);
  54.     }
  55.  
  56.     Object.values(resourceMeta.properties).forEach(async (propertyMeta) => {
  57.         if (((propertyMeta.isOutsideEditable && isCalledFromOutside) || !isCalledFromOutside)
  58.             && propertyMeta.type !== PropertyType.Object
  59.             && propertyMeta.type !== PropertyType.Array) {
  60.            
  61.             const newPropertyValue = propertyMetaReflectionService.getPropertyValue(resource, propertyMeta);
  62.             const existingPropertyValue = propertyMetaReflectionService.getPropertyValue(resourceFromDb, propertyMeta);
  63.  
  64.             if ((newPropertyValue !== null && !isEqual(newPropertyValue, existingPropertyValue)) ||
  65.                 (existingPropertyValue !== null && !isEqual(existingPropertyValue, newPropertyValue))) {
  66.                 await permissionChecker.checkPropertyPermission(resourceMeta, CrudAction.Update, userId,
  67.                     resource, propertyMeta.id);
  68.             }
  69.  
  70.             propertyMetaReflectionService.setPropertyValueFromAnotherResource(resourceFromDb, resource, propertyMeta);
  71.         }
  72.     });
  73.  
  74.     if (editBaseResourceService.isBaseResource(clrType)) {
  75.         await editBaseResourceService.setModifiedProperties(resourceFromDb as IBaseResource, userId);
  76.     }
  77.  
  78.     return resourceFromDb;
  79. }
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement