Partition messages by date, last read in angular - javascript

I want to partition my messages by date for my chat application(Similar to the Microsoft teams app)
The message data will be like
[
{
"id": 577,
"source": {
"userID": 56469,
"profilePictureUrl": "",
"name": "John J"
},
"body": "test test",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T07:59:28.873+00:00"
},
{
"id": 578,
"source": {
"userID": 56469,
"profilePictureUrl": "",
"name": "Don V"
},
"body": "ok",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T08:02:26.262+00:00"
},
{
"id": 628,
"source": {
"userID": 56470,
"profilePictureUrl": "",
"name": "Sam GP"
},
"body": "Hola",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:27:48.038+00:00"
},
{
"id": 629,
"source": {
"userID": 56470,
"profilePictureUrl": "",
"name": "Rawn OP"
},
"body": "ek",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:29:36.705+00:00"
},
{
"id": 630,
"source": {
"userID": 56470,
"profilePictureUrl": "",
"name": "Paul John"
},
"body": "hi",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:30:36.695+00:00"
},
{
"id": 631,
"source": {
"userID": 56470,
"profilePictureUrl": "",
"name": "Dennise V"
},
"body": "knock knock",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:32:38.035+00:00"
},
{
"id": 632,
"source": {
"userID": 56469,
"profilePictureUrl": "",
"name": "Shawn"
},
"body": "who's this",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:37:25.985+00:00"
},
{
"id": 633,
"source": {
"userID": 56469,
"profilePictureUrl": "",
"name": "Pater B"
},
"body": "I see",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:37:30.783+00:00"
},
{
"id": 634,
"source": {
"userID": 56469,
"profilePictureUrl": "",
"name": "Cera LO"
},
"body": "Will call you later",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-20T17:37:38.268+00:00"
},
{
"id": 642,
"source": {
"userID": 56469,
"profilePictureUrl": "",
"name": "Rose BH"
},
"body": "hello???????",
"readStatus": true,
"attachments": null,
"createdDateTime": "2022-09-21T05:25:56.642+00:00"
}
]
I need to arrange these data to show the messages by date like
------------------------------Sep 30-----------------------------
Messages sent/received on sep 30
------------------------------Yesterday--------------------------
Messages sent/received on yesterday
------------------------------Last read-------------------------
------------------------------Oct30-----------------------------
------------------------------Yesterday-------------------------
------------------------------Today-----------------------------
For displaying "Sep 30", "yesterday", and "Today" I've created a pipe that converts the timestamp into the month and "yesterday", "today" etc.
I already got the solution to arrange the message by date. But I have to arrange it under the "Last read" block too. Same as by dates. the flag "readStatus" is sed to check whether the message has been read or not" If it is false is should come under "Last read".
Any ideas? Any help will be appreciated.

I imagine you can has something like (if your data is in a variable "data"
dataOrder = {
readed: this.group(this.data.filter((x) => x.readStatus)),
notReaded: this.group(this.data.filter((x) => !x.readStatus)),
};
group(data: any[]) {
return data.reduce((a, b) => {
const dateTime=new Date(b.createdDateTime.substr(0,10)+"T00:00:00.00+00:00")
.getTime()
const element = a.find((x) => x.dateTime == dateTime);
if (!element) a.push({ dateTime:dateTime,data: [b] });
else element.data.push(b);
return a;
}, []);
}
See that "dataOrder" has two properties "readed" and "notReaded", each one are arrays of objects in the way
{
dateTime:a dateTime,
data:an array with the messages
}
This makes easy indicate the date using Angular date pipe, and loop over the messages
So, an .html like
<h1>Readed</h1>
<ng-container
*ngTemplateOutlet="messagedata; context: { $implicit: dataOrder.readed }"
></ng-container>
<h1>Not Readed</h1>
<ng-container
*ngTemplateOutlet="messagedata; context: { $implicit: dataOrder.notReaded }"
></ng-container>
<ng-template #messagedata let-data>
<div *ngFor="let readed of data">
{{ readed.dateTime | date }}
<div *ngFor="let message of readed.data">
{{ message.createdDateTime }} - {{ message.body }}
</div>
</div>
</ng-template>
I use the same template for both type of messages
A stackblitz
Update As the dataOrder it's only change at fisrt is better use a function
getDataOrdered(data:any[])
{
return {
readed: this.group(data.filter((x) => x.readStatus)),
notReaded: this.group(data.filter((x) => !x.readStatus)),
};
}
Then, we can, each time some message change its "readStatus" we can do
this.dataOrder=this.getDataOrdered(this.data)

Related

How to sort nested array depending on values in the inner array?

I have an array of To-Do lists with an array of tasks inside of the array. I now need to sort the array depending on the value of the inner array. I want the array to be sorted by highest priority on the important attribute first. Then remove all todos that have a dueDateTime (if it exists) that are older than today (Because Microsoft Graph API returns all tasks in a separate list of todos, and don't exclude older tasks). todos that don't have a dueDateTime should not be removed from the array. The existing values should be sorted by the dueDateTime value.
How can I achieve this in JavaScript?
This is my array with all the tasks that Microsoft Graph API returns:
let todos = [{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('f7dec6a0-3f91-43e2-8a28-d6de2f238caa')/todo/lists('AQMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OAAALgAAA7JK6Wv8LyFEhk0vxTmAtLMBANEaHwdeaklLnbQ6yUS32Z0AAAIBEgAAAA%3D%3D')/tasks",
"value": [{
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbfP5Q==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "test12341241241241451",
"createdDateTime": "2021-11-10T09:20:54.6522448Z",
"lastModifiedDateTime": "2021-11-12T06:23:04.4839213Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAAAAAESAADRGh8HXmpJS520OslEt9mdAAADfurHAAA=",
"body": {
"content": "",
"contentType": "text"
},
"dueDateTime": {
"dateTime": "2021-11-12T23:00:00.0000000",
"timeZone": "UTC"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbefnQ==\"",
"importance": "high",
"isReminderOn": false,
"status": "notStarted",
"title": "test 1231551515155",
"createdDateTime": "2021-11-10T09:20:51.5538432Z",
"lastModifiedDateTime": "2021-11-11T09:19:14.5876034Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAAAAAESAADRGh8HXmpJS520OslEt9mdAAADfurGAAA=",
"body": {
"content": "",
"contentType": "text"
},
"dueDateTime": {
"dateTime": "2021-11-10T23:00:00.0000000",
"timeZone": "UTC"
}
}]
}, {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('f7dec6a0-3f91-43e2-8a28-d6de2f238caa')/todo/lists('AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OAAuAAAAAACySulr-C8hRIZNL8U5gLSzAQDRGh8HXmpJS520OslEt9mdAAADfg5CAAA%3D')/tasks",
"value": [{
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeZZg==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "test test testtest test testtest test testtest test testtest test testtest test testtest test test'",
"createdDateTime": "2021-11-11T06:55:42.8810298Z",
"lastModifiedDateTime": "2021-11-11T07:05:43.9657845Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAAFujyNAAA=",
"body": {
"content": "",
"contentType": "text"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeZZA==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "test test testtest test testtest test testtest test test",
"createdDateTime": "2021-11-11T06:55:40.2346649Z",
"lastModifiedDateTime": "2021-11-11T07:05:41.8202032Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAAFujyMAAA=",
"body": {
"content": "",
"contentType": "text"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeflQ==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "detta är ett test",
"createdDateTime": "2021-11-11T06:55:30.6219299Z",
"lastModifiedDateTime": "2021-11-11T09:19:13.9912155Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAAFujyLAAA=",
"body": {
"content": "",
"contentType": "text"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeflw==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "Testar filips lista",
"createdDateTime": "2021-11-10T09:21:52.9503933Z",
"lastModifiedDateTime": "2021-11-11T09:19:14.1395668Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAADfvKlAAA=",
"body": {
"content": "",
"contentType": "text"
}
}]
}, {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('f7dec6a0-3f91-43e2-8a28-d6de2f238caa')/todo/lists('AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OAAuAAAAAACySulr-C8hRIZNL8U5gLSzAQDRGh8HXmpJS520OslEt9mdAAADfg5BAAA%3D')/tasks",
"value": [{
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbefmQ==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "Detta är ett test i test",
"createdDateTime": "2021-11-10T09:21:45.376632Z",
"lastModifiedDateTime": "2021-11-11T09:19:14.2918996Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5BAADRGh8HXmpJS520OslEt9mdAAADfu66AAA=",
"body": {
"content": "",
"contentType": "text"
}
}]
}];
Update:
I tried the first answers code from #Tom and got the error:
"Uncaught TypeError: item.dueDateTime.dateTime.getTime is not a
function",
I also tried to use new Date (item.dueDateTime.dateTime) to convert it to a valid date format but it did not work..
Update 2
I tried the answers updated code and i get the error:
"Uncaught TypeError: Cannot read properties of undefined (reading 'dateTime')"
let todos = [{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('f7dec6a0-3f91-43e2-8a28-d6de2f238caa')/todo/lists('AQMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OAAALgAAA7JK6Wv8LyFEhk0vxTmAtLMBANEaHwdeaklLnbQ6yUS32Z0AAAIBEgAAAA%3D%3D')/tasks",
"value": [{
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbfP5Q==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "test12341241241241451",
"createdDateTime": "2021-11-10T09:20:54.6522448Z",
"lastModifiedDateTime": "2021-11-12T06:23:04.4839213Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAAAAAESAADRGh8HXmpJS520OslEt9mdAAADfurHAAA=",
"body": {
"content": "",
"contentType": "text"
},
"dueDateTime": {
"dateTime": "2021-11-12T23:00:00.0000000",
"timeZone": "UTC"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbefnQ==\"",
"importance": "high",
"isReminderOn": false,
"status": "notStarted",
"title": "test 1231551515155",
"createdDateTime": "2021-11-10T09:20:51.5538432Z",
"lastModifiedDateTime": "2021-11-11T09:19:14.5876034Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAAAAAESAADRGh8HXmpJS520OslEt9mdAAADfurGAAA=",
"body": {
"content": "",
"contentType": "text"
},
"dueDateTime": {
"dateTime": "2021-11-10T23:00:00.0000000",
"timeZone": "UTC"
}
}]
}, {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('f7dec6a0-3f91-43e2-8a28-d6de2f238caa')/todo/lists('AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OAAuAAAAAACySulr-C8hRIZNL8U5gLSzAQDRGh8HXmpJS520OslEt9mdAAADfg5CAAA%3D')/tasks",
"value": [{
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeZZg==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "test test testtest test testtest test testtest test testtest test testtest test testtest test test'",
"createdDateTime": "2021-11-11T06:55:42.8810298Z",
"lastModifiedDateTime": "2021-11-11T07:05:43.9657845Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAAFujyNAAA=",
"body": {
"content": "",
"contentType": "text"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeZZA==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "test test testtest test testtest test testtest test test",
"createdDateTime": "2021-11-11T06:55:40.2346649Z",
"lastModifiedDateTime": "2021-11-11T07:05:41.8202032Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAAFujyMAAA=",
"body": {
"content": "",
"contentType": "text"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeflQ==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "detta är ett test",
"createdDateTime": "2021-11-11T06:55:30.6219299Z",
"lastModifiedDateTime": "2021-11-11T09:19:13.9912155Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAAFujyLAAA=",
"body": {
"content": "",
"contentType": "text"
}
}, {
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbeflw==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "Testar filips lista",
"createdDateTime": "2021-11-10T09:21:52.9503933Z",
"lastModifiedDateTime": "2021-11-11T09:19:14.1395668Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5CAADRGh8HXmpJS520OslEt9mdAAADfvKlAAA=",
"body": {
"content": "",
"contentType": "text"
}
}]
}, {
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('f7dec6a0-3f91-43e2-8a28-d6de2f238caa')/todo/lists('AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OAAuAAAAAACySulr-C8hRIZNL8U5gLSzAQDRGh8HXmpJS520OslEt9mdAAADfg5BAAA%3D')/tasks",
"value": [{
"#odata.etag": "W/\"0RofB15qSUudtDrJRLfZnQAABbefmQ==\"",
"importance": "normal",
"isReminderOn": false,
"status": "notStarted",
"title": "Detta är ett test i test",
"createdDateTime": "2021-11-10T09:21:45.376632Z",
"lastModifiedDateTime": "2021-11-11T09:19:14.2918996Z",
"id": "AAMkAGU0Yjc1Y2U4LWE3ODYtNGE3YS04ZjVlLTQxYjlmNDZkYWM4OABGAAAAAACySulr-C8hRIZNL8U5gLSzBwDRGh8HXmpJS520OslEt9mdAAADfg5BAADRGh8HXmpJS520OslEt9mdAAADfu66AAA=",
"body": {
"content": "",
"contentType": "text"
}
}]
}];
let newArray = [];
// flatten the array so we can use Array.sort()
todos.forEach(list => {
list.value.forEach(item => {
newArray.push(item);
})
});
// filter out anything older than 1 day
newArray = newArray.filter((item) => {
return new Date(item.dueDateTime.dateTime).getTime() + 86400000 < new Date().getTime(); // 8.64 million = 1 day
});
// sort the array
newArray = newArray.sort((a, b) => {
// convert importance to numbers so we can compare
const aScore = a.importance === 'high' ? 1 : 0;
const bScore = b.importance === 'high' ? 1 : 0;
// sorts by importance. If the importances are equal a-b is 0, so
// instead we sort by dates, putting the lowest first
return aScore - bScore || !b.dueDateTime ? 1 : !a.dueDateTime ? -1 : new Date(b.dueDateTime.dateTime).getTime() - new Date(a.dueDateTime.dateTime).getTime();
});
It will probably be easier for you to flatten your array first and then run a sort function on it.
let newArray = [];
// flatten the array so we can use Array.sort()
todos.forEach((list) => {
list.forEach((item) => {
newArray.push(item);
}
}
// filter out anything older than 1 day
newArray = newArray.filter((item) => {
return !item.dueDateTime || new Date(item.dueDateTime.dateTime).getTime() + 86400000 < new Date().getTime(); // 8.64 million = 1 day
});
// sort the array
newArray = newArray.sort((a, b) => {
// convert importance to numbers so we can compare
const aScore = a.importance === 'high' ? 1 : 0;
const bScore = b.importance === 'high' ? 1 : 0;
// sorts by importance. If the importances are equal a-b is 0, so
// instead we sort by dates, putting the lowest first
return aScore - bScore || !b.dueDateTime ? 1 : !a.dueDateTime ? -1 : new Date(b.dueDateTime.dateTime).getTime() - new Date(a.dueDateTime.dateTime).getTime();
});

mongoDB - How do master-detail structure grouped detail collection specific fields summary

In a mongodb database with a master-detail structure, how do I ensure that the data in the detail table is grouped according to the status area while querying the master table. For example ...
Master, Row, Job, Fields, [Detail Status Grouped Count / Summary Object]
Edited:
sms_jobs collection example data is :
// 1
{
"_id": NumberInt("1"),
"company": {
"id": NumberInt("1"),
"name": "",
"sms_gateway_id": NumberInt("1"),
"sms_gateway_parameters": {
"password": "",
"username": ""
},
"is_active": true
},
"source_type": "form",
"source": {
"id": NumberInt("2"),
"company_id": NumberInt("1"),
"title": "Import Data Demo",
"description": "<p>Üst Açıklama</p>",
"footer_description": "<p>Alt Açıklama</p>",
"thanks_message": "Katiliminiz icin tesekkur ederiz.",
"sms_message": "{company} KVKK Aydinlatma Metni ve Acik Riza Onayi icin Dogrulama Kodunuz : {code} ( {form_link} )",
"digest": null,
"ts_digest": null,
"is_active": true,
"is_locked": false
},
"description": "Import Data Demo",
"status": "waiting",
"job_count": NumberInt("22145"),
"updated_at": ISODate("2020-08-15T17:00:49.252Z"),
"created_at": ISODate("2020-08-15T16:59:10.899Z")
}
sms_job_details collection example data is :
// 1
{
"_id": NumberInt("221462"),
"sms_job_id": NumberInt("1"),
"gsm": "",
"schedule_at": ISODate("2020-08-15T19:41:44.827Z"),
"raw_message": "{name} {surname} {id_number} {form_link} {reject_link}",
"message": "",
"status": "waiting", // I want this statuses grouped count my master table rows in line object
"expire_at": "2020-08-18T17:40:44.967Z",
"updated_at": ISODate("2020-08-15T18:45:53.727Z"),
"created_at": ISODate("2020-08-15T18:45:53.727Z")
}
// 2
{
"_id": NumberInt("221463"),
"sms_job_id": NumberInt("1"),
"gsm": "",
"schedule_at": ISODate("2020-08-15T19:41:44.827Z"),
"raw_message": "{name} {surname} {id_number} {form_link} {reject_link}",
"message": "",
"status": "failed", // I want this statuses grouped count my master table rows in line object
"expire_at": "2020-08-18T17:40:44.967Z",
"updated_at": ISODate("2020-08-15T18:45:53.727Z"),
"created_at": ISODate("2020-08-15T18:45:53.727Z")
}
// 3
{
"_id": NumberInt("221464"),
"sms_job_id": NumberInt("1"),
"gsm": "",
"schedule_at": ISODate("2020-08-15T19:41:44.827Z"),
"raw_message": "{name} {surname} {id_number} {form_link} {reject_link}",
"message": "",
"status": "success", // I want this statuses grouped count my master table rows in line object
"expire_at": "2020-08-18T17:40:44.967Z",
"updated_at": ISODate("2020-08-15T18:45:53.727Z"),
"created_at": ISODate("2020-08-15T18:45:53.727Z")
}
I experimented for approximately 4-5 hours. I could get the closest result with the following query.
db.getCollection("sms_jobs").aggregate([{
$lookup: {
from: "sms_job_details",
localField: "_id",
foreignField: "sms_job_id",
as: "data"
}
}, {
$unwind: "$data"
}, {
$group: {
_id: {
id: "$_id",
status: "$data.status"
},
qty: {
$sum: 1
}
}
}, {
$project: {
_id: "$_id.id",
qty: 1,
status: "$_id.status"
}
}])
And get this result :
// 1
{
"qty": 22145,
"_id": NumberInt("4"),
"status": "success"
}
// 2
{
"qty": 1,
"_id": NumberInt("3"),
"status": "success"
}
// 3
{
"qty": 22142,
"_id": NumberInt("1"),
"status": "success"
}
// 4
{
"qty": 1,
"_id": NumberInt("1"),
"status": "failed"
}
// 5
{
"qty": 2,
"_id": NumberInt("1"),
"status": "waiting"
}
// 6
{
"qty": 1,
"_id": NumberInt("3"),
"status": "failed"
}
// 7
{
"qty": 3,
"_id": NumberInt("3"),
"status": "waiting"
}
What do I want ?
The query result I want is as follows.
// 1
{
"_id": NumberInt("1"),
"company": {
"id": NumberInt("1"),
"name": "",
"sms_gateway_id": NumberInt("1"),
"sms_gateway_parameters": {
"password": "",
"username": ""
},
"is_active": true
},
"source_type": "form",
"source": {
"id": NumberInt("2"),
"company_id": NumberInt("1"),
"title": "Import Data Demo",
"description": "<p>Üst Açıklama</p>",
"footer_description": "<p>Alt Açıklama</p>",
"thanks_message": "Katiliminiz icin tesekkur ederiz.",
"sms_message": "{company} KVKK Aydinlatma Metni ve Acik Riza Onayi icin Dogrulama Kodunuz : {code} ( {form_link} )",
"digest": null,
"ts_digest": null,
"is_active": true,
"is_locked": false
},
"description": "Import Data Demo",
"status": "waiting",
"job_count": NumberInt("22145"),
"updated_at": ISODate("2020-08-15T17:00:49.252Z"),
"created_at": ISODate("2020-08-15T16:59:10.899Z"),
// Added part...
"status_countes": {
"success": NumberInt("20"),
"failed": NumberInt("2"),
"waiting": NumberInt("11"),
"canceled": NumberInt("0"),
"total": NumberInt("33")
},
}
Thank you in advance for your help...

Jsonata module usage in NodeJS Project

I have tried jsonata exerciser. It looks cool.
But I have to implement it in my code(NodeJS).
Let's say
Input is:
{
"id": "course_uuid1",
"description": "Sample course description",
"contentType": "COURSE",
"category": "Course",
"durationInSeconds": 500,
"expertiseLevels": ["INTERMEDIATE"],
"imageUrl": "https://percipio.com/courseuuid1/imagelink",
"link": "https://percipio.com/courseuuid1",
"modalities": ["LISTEN", "READ", "WATCH"],
"languageCode": "en",
"parent": null,
"publishDate": "2018-11-19T10:23:34Z",
"sourceName": null,
"technologyTitle": null,
"technologyVersion": null,
"title": "Java",
"by": ["admin"]
}
Transformation logic is:
{
"pkID": id,
"description": description,
"componentTypeID": contentType,
"totalLength": durationInSeconds,
"thumbnailURI": imageUrl,
"locale": languageCode,
"createTimestamp": publishDate,
"title": title,
"lastUpdateUser": by
}
I want output in the transformation logic format, but through NODEJS code.
Please suggest
Install jsonata node module and try below code:
var jsonata = require('jsonata');
let input=
{
"id": "course_uuid1",
"description": "Sample course description",
"contentType": "COURSE",
"category": "Course",
"durationInSeconds": 500,
"expertiseLevels": ["INTERMEDIATE"],
"imageUrl": "https://percipio.com/courseuuid1/imagelink",
"link": "https://percipio.com/courseuuid1",
"modalities": ["LISTEN", "READ", "WATCH"],
"languageCode": "en",
"parent": null,
"publishDate": "2018-11-19T10:23:34Z",
"sourceName": null,
"technologyTitle": null,
"technologyVersion": null,
"title": "Java",
"by": ["admin"]
}
let exp="{'pkID': id,'description': description,'componentTypeID': contentType,'totalLength': durationInSeconds,'thumbnailURI': imageUrl,'locale': languageCode,'createTimestamp': publishDate,'title': title,'lastUpdateUser': by}";
let expression = jsonata(exp);
let expResult = expression.evaluate(input);
console.log(expResult);
I think this is what you want:
var inputJson={
"id": "course_uuid1",
"description": "Sample course description",
"contentType": "COURSE",
"category": "Course",
"durationInSeconds": 500,
"expertiseLevels": ["INTERMEDIATE"],
"imageUrl": "https://percipio.com/courseuuid1/imagelink",
"link": "https://percipio.com/courseuuid1",
"modalities": ["LISTEN", "READ", "WATCH"],
"languageCode": "en",
"parent": null,
"publishDate": "2018-11-19T10:23:34Z",
"sourceName": null,
"technologyTitle": null,
"technologyVersion": null,
"title": "Java",
"by": ["admin"]
};
var outputJson={
"pkID": inputJson.id,
"description": inputJson.description,
"componentTypeID": inputJson.contentType,
"totalLength": inputJson.durationInSeconds,
"thumbnailURI": inputJson.imageUrl,
"locale": inputJson.languageCode,
"createTimestamp": inputJson.publishDate,
"title": inputJson.title,
"lastUpdateUser": inputJson.by
}

FooTable plugin datetime issue

I have created a dynamic table using FooTable jQuery Plugin. http://jsbin.com/wasawa/edit
Here I used MySQL timestamp using PHP date('Y-m-d H:i:s') format. e.g- 2016-01-19 01:22:13, but using the FooTable I'm not getting the actual date format.
Any help would be appreciated.
Updated the columns object like below:
{
"name": "createdat",
"title": "Created On",
"formatter": function(value){
return moment(value).format('MMM Do YY');
}
}
I have to update the formatter function.
And now it is working fine as expected.
Thanks for your support.
In case this helps someone else with the same problem. I was able to get the example from the site, using the following. You must include moment.js for this to work. https://momentjs.com/
This code won't run in SO's code runner because footable requires localstorage.
It displays the unix time from an external source as whatever you want. I have it using MMM Do YY in this example.
jQuery(function($) {
$('#showcase-example-22').footable({
"useParentWidth": true,
columns: [
{
"name": "id",
"title": "ID",
"breakpoints": "xs sm",
"type": "number",
"style": {
"width": 80,
"maxWidth": 80
}
},
{
"name": "firstName",
"title": "First Name"
},
{
"name": "lastName",
"title": "Last Name"
},
{
"name": "something",
"title": "Never seen but always around",
"visible": false,
"filterable": false
},
{
"name": "jobTitle",
"title": "Job Title",
"breakpoints": "xs sm",
"style": {
"maxWidth": 200,
"overflow": "hidden",
"textOverflow": "ellipsis",
"wordBreak": "keep-all",
"whiteSpace": "nowrap"
}
},
{
"name": "started",
"title": "Started On",
"type": "numeric",
"breakpoints": "xs sm md"
},
{
"name": "dob",
"title": "Date of Birth",
"formatter": function(value) {
var datetime = JSON.parse(value)
return moment(datetime).format("MMM Do YY")
}
},
{
"name": "status",
"title": "Status"
}
],
rows: [{
"id": 1,
"firstName": "Annemarie",
"lastName": "Bruening",
"something": 1381105566987,
"jobTitle": "Cloak Room Attendant",
"started": 1367700388909,
"dob": 122365714987,
"status": "Suspended"
},
{
"id": 2,
"firstName": "Nelly",
"lastName": "Lusher",
"something": 1267237540208,
"jobTitle": "Broadcast Maintenance Engineer",
"started": 1382739570973,
"dob": 183768652128,
"status": "Disabled"
},
{
"id": 3,
"firstName": "Lorraine",
"lastName": "Kyger",
"something": 1263216405811,
"jobTitle": "Geophysicist",
"started": 1265199486212,
"dob": 414197000409,
"status": "Active"
},
{
"id": 4,
"firstName": "Maire",
"lastName": "Vanatta",
"something": 1317652005631,
"jobTitle": "Gaming Cage Cashier",
"started": 1359190254082,
"dob": 381574699574,
"status": "Disabled"
},
{
"id": 5,
"firstName": "Whiney",
"lastName": "Keasler",
"something": 1297738568550,
"jobTitle": "High School Librarian",
"started": 1377538533615,
"dob": -11216050657,
"status": "Active"
},
{
"id": 6,
"firstName": "Nikia",
"lastName": "Badgett",
"something": 1283192889859,
"jobTitle": "Clown",
"started": 1348067291754,
"dob": -236655382175,
"status": "Active"
}
]
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-footable/3.1.4/footable.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-footable/3.1.4/footable.bootstrap.css" rel="stylesheet" />
<table id="showcase-example-1" class="table" data-paging="true" data-filtering="true" data-sorting="true" data-editing="true" data-state="true"></table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-footable/3.1.4/footable.js"></script>
enter code here

Passing function argument to retrieve data from an object

I am have some trouble with a script I am working on. I have been provided with an object with multiple items from a product catalog.
What I am trying to do is to write a function which to which will allow me to render this data easily.
<script type="application/javascript">
SKUinfo =
{
"s238554": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/icon18.gif"
},
"Barcode": {
"Barcode": "50622132430794"
},
"Currency": "£",
"Description": "Description goes here",
"Id": 44305,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "Xbox 360",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
}
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
},
"s238556": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/pegi/icon18.gif"
},
"Barcode": {
"Barcode": "5060134530794"
},
"Currency": "£",
"Description": "Description here",
"Id": 654654,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "PlayStation 3",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
},
{
"ScreenshotMax": "/productImages/238556/7scrmax2.jpg",
"ScreenshotMin": "/productImages/238556/6scrmin2.jpg"
},
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
"VideoHTML": "html here",
"status": {
"Response": "product found",
"Success": true
}
}
}
</script>
The above example is the output I get for two products.
If I try to get access to this data this is where I have a problem
<script type="application/javascript">
function getSKU(s)
{
console.log(SKUinfo.s.Title);
}
getSKU(s238554);
</script>
I imagine this is being caused when I am passing the argument s back to the function getSKU a the node selection in the data object. In this I would expect the console output to be the Title from SKU s238554.
What I get however, is: Uncaught ReferenceError: s238554 is not defined
I would appreciate any guidance that can be offered as I am a javascript novice.
Access your property by used[] on SKUinfo.s.Title like SKUinfo[s].Title
And also pass your property name within the quotes 's238554' as it's not variable.
Something like this.
function getSKU(s){
console.log(SKUinfo[s].Title);
}
getSKU('s238554'); // s238554 within quotes.

Categories

Resources