I have an arrangement that I need to take an average of a sum based on the user who attended an activity
I have an array where there is a list of activities, so suppose that the activity of the day "2022-11-25", which was a Friday, was attended by the entire organization, so the status isAttendanceAllOrg is true for Therefore, the usersNotAttendance array is empty, but on "2022-11-26" which was a Saturday, the entire organization did not attend, therefore the isAttendanceAllOrg status would go to false and in usersNotAttendance we would bring the users who did not attend the activity. Now we need to assign the minimum hours for each day, respectively.
But to do so we must take into account which modality the user belongs to. If the user is Fulltime, we use minTracked, but if it is part time, we will use minTrackedPartime. That is, the day "2022-11-25", which was a Friday. It was established that the minimum number of hours in Fulltime was "minTracked: 6" and the half-time ones were "minTrackedPartime: 2.5" and on Saturday a "minTracked: 3" for Fulltime and for Parttime a "minTrackedPartime: 3", so we need to find a way to average out minTracked and minTrackedPartime based on users who attended or not.
For example, as the day "2022-11-25" the user with the ID "1267" which is part time attended the activity, but the day "2022-11-26" did not, then his final average would be "totalTracked: 2.5", since the "minTrackedPartime" of the day "2022-11-26" is not being added because he did not attend. Quite the contrary, for that user, who did attend both activities on both days and that is full time, his average final would be the minTracked of date 25 plus the minTracked of date 26 which would be equal to 6 + 3 = 9.
So I have to be able to get a list of all the users with pertinent information on the modalities and their ID:
[
{
"id": 1278,
"name": "User18",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1273,
"username": "User17",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 31,
"tittle": "SAC",
"priority": 1,
"type": 2,
"created_at": "2022-11-02T21:05:33+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1288,
"username": "User16",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1279,
"username": "User15",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1287,
"username": "User14",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1277,
"username": "User13",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1270,
"username": "User12",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 2,
"job_position": "Junior",
"created_at": "2022-11-02T21:53:41.024661+00:00",
"status": false
}
},
{
"id": 1272,
"username": "User11",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 31,
"tittle": "SAC",
"priority": 1,
"type": 2,
"created_at": "2022-11-02T21:05:33+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1282,
"name": "User10",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1266,
"username": "User09",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 5,
"job_position": "Senior",
"created_at": "2022-11-02T21:54:33.127234+00:00",
"status": true
}
},
{
"id": 1276,
"username": "User08",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 2,
"job_position": "Junior",
"created_at": "2022-11-02T21:53:41.024661+00:00",
"status": false
}
},
{
"id": 1264,
"username": "User08",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 5,
"job_position": "Senior",
"created_at": "2022-11-02T21:54:33.127234+00:00",
"status": true
}
},
{
"id": 1281,
"username": "User07",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 31,
"tittle": "SAC",
"priority": 1,
"type": 2,
"created_at": "2022-11-02T21:05:33+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1268,
"username": "User06",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 2,
"job_position": "Junior",
"created_at": "2022-11-02T21:53:41.024661+00:00",
"status": false
}
},
{
"id": 1269,
"username": "User05",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 2,
"job_position": "Junior",
"created_at": "2022-11-02T21:53:41.024661+00:00",
"status": false
}
},
{
"id": 1265,
"username": "User04",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 5,
"job_position": "Senior",
"created_at": "2022-11-02T21:54:33.127234+00:00",
"status": true
}
},
{
"id": 1292,
"username": "User03",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 32,
"tittle": "DESIGN",
"priority": 2,
"type": 3,
"created_at": "2022-11-02T21:05:51+00:00",
"status": true
},
"job_position": {
"id": 4,
"job_position": "Trainee Fulltime",
"created_at": "2022-11-02T21:54:17.963682+00:00",
"status": true
}
},
{
"id": 1274,
"username": "User02",
"modality": {
"id": 1,
"name": "FULLTIME",
"weekly_hours": 44,
"daily_hours": 8,
"monthly_hours": 192,
"created_at": "2022-11-02T20:46:07.169271+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 2,
"job_position": "Junior",
"created_at": "2022-11-02T21:53:41.024661+00:00",
"status": false
}
},
{
"id": 1267,
"username": "User01",
"modality": {
"id": 2,
"name": "PARTTIME",
"weekly_hours": 24,
"daily_hours": 4,
"monthly_hours": 104,
"created_at": "2022-11-02T20:47:13.961156+00:00",
"status": true
},
"rol": {
"id": 30,
"tittle": "DEV",
"priority": 0,
"type": 1,
"created_at": "2022-11-02T21:05:08+00:00",
"status": true
},
"job_position": {
"id": 5,
"job_position": "Senior",
"created_at": "2022-11-02T21:54:33.127234+00:00",
"status": true
}
}
]
And a list of activities
[
{
"id": 18,
"created_at": "2022-11-30T22:07:29.47307+00:00",
"date_start": "2022-11-25",
"date_end": "2022-11-25",
"indicator_hours": 3,
"id_org": 1,
"minTracked": 6,
"minActivity": 20,
"isAttendanceAllOrg": true,
"usersNotAttendance": [],
"description": "ACTIVIDAD DE PRUEBA",
"type_of_schedule": "activity",
"indicator_activity": 2,
"minTrackedPartime": 2.5,
"minActivityPartime": 10
},
{
"id": 20,
"created_at": "2022-11-30T22:41:16.081726+00:00",
"date_start": "2022-11-26",
"date_end": "2022-11-26",
"indicator_hours": 3,
"id_org": 1,
"minTracked": 3,
"minActivity": 10,
"isAttendanceAllOrg": false,
"usersNotAttendance": [
{
"isWork": true,
"id_user": 1267
},
{
"isWork": true,
"id_user": 1274
},
{
"isWork": true,
"id_user": 1292
}
],
"description": "ACTIVIDAD SIN ASISTENTES",
"type_of_schedule": "activity",
"indicator_activity": 3,
"minTrackedPartime": 3,
"minActivityPartime": 20
}
]
And my output should be something like:
[{
"user_id": 1267,
"username": "User01",
"modality": "PARTTIME",
"totalTracked": 2.5
},
{
"user_id": 1274,
"username": "User02",
"modality": "FULLTIME",
"totalTracked": 6
},
{
"user_id": 1292,
"username": "User03",
"modality": "FULLTIME",
"totalTracked": 6
},
{
"user_id": 1268,
"username": "User06",
"modality": "FULLTIME",
"totalTracked": 9
},
{
"user_id": 1264,
"username": "User08",
"modality": "FULLTIME",
"totalTracked": 9
},
{
"user_id": 1277,
"username": "User13",
"modality": "FULLTIME",
"totalTracked": 9
}
.
.
.
{
"user_id": 1278,
"username": "User18",
"modality": "FULLTIME",
"totalTracked": 9
}]
I was thinking that maybe with a reduce in the array, where we are filtering by the user ID like they did in this example Reduce array of objects by user_id and sum certain values, but since the id that I am accessing is inside usersNotAttendance it is difficult for me to understand how to filter it in such a way that it can be accessed.