added chat log model and serializer

This commit is contained in:
Ronald A. Richardson
2024-04-08 09:46:00 +08:00
parent 7281174d12
commit 7874edf0f3
4 changed files with 77 additions and 5 deletions

View File

@@ -3,14 +3,16 @@ import { computed } from '@ember/object';
import { getOwner } from '@ember/application';
import { format, formatDistanceToNow, isValid as isValidDate } from 'date-fns';
export default class ChatChannel extends Model {
export default class ChatChannelModel extends Model {
/** @ids */
@attr('string') public_id;
@attr('string') company_uuid;
/** @attributes */
@attr('string') name;
@attr('string') title;
@attr('string') slug;
@attr('array') feed;
@attr('array') meta;
/** @relationships */
@@ -18,7 +20,7 @@ export default class ChatChannel extends Model {
@hasMany('chat-message', { async: false }) messages;
@hasMany('chat-attachment', { async: true }) attachments;
@hasMany('chat-presence', { async: true }) presences;
@belongsTo('chat-message', { async: false }) lastMessage;
@belongsTo('chat-message', { async: false }) last_message;
/** @dates */
@attr('date') created_at;
@@ -64,7 +66,7 @@ export default class ChatChannel extends Model {
return store.query('chat-participant', { chat_channel_uuid: this.id }).then((participants) => {
this.set('participants', participants);
return participants
return participants;
});
}
@@ -74,7 +76,7 @@ export default class ChatChannel extends Model {
return store.query('chat-message', { chat_channel_uuid: this.id }).then((messages) => {
this.set('messages', messages);
return messages
return messages;
});
}
@@ -84,7 +86,7 @@ export default class ChatChannel extends Model {
return store.query('chat-attachment', { chat_channel_uuid: this.id }).then((attachments) => {
this.set('attachments', attachments);
return attachments
return attachments;
});
}
}

View File

@@ -0,0 +1,55 @@
import Model, { belongsTo } from '@ember-data/model';
import { computed } from '@ember/object';
import { getOwner } from '@ember/application';
import { format, formatDistanceToNow, isValid as isValidDate } from 'date-fns';
export default class ChatLogModel extends Model {
/** @ids */
@attr('string') company_uuid;
@attr('string') chat_channel_uuid;
@attr('string') initiator_uuid;
/** @attributes */
@attr('string') content;
@attr('string') resolved_content;
@attr('string') event_type;
@attr('string') status;
@attr('array') meta;
/** @dates */
@attr('date') created_at;
@attr('date') updated_at;
/** @computed */
@computed('updated_at') get updatedAgo() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDistanceToNow(this.updated_at);
}
@computed('updated_at') get updatedAt() {
if (!isValidDate(this.updated_at)) {
return null;
}
return formatDate(this.updated_at, 'PP HH:mm');
}
@computed('created_at') get createdAgo() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDistanceToNow(this.created_at);
}
@computed('created_at') get createdAt() {
if (!isValidDate(this.created_at)) {
return null;
}
return formatDate(this.created_at, 'PP HH:mm');
}
}

View File

@@ -11,6 +11,7 @@ export default class ChatChannelSerializer extends ApplicationSerializer.extend(
return {
participants: { embedded: 'always' },
messages: { embedded: 'always' },
last_message: { embedded: 'always' },
};
}
}

View File

@@ -0,0 +1,14 @@
import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
module('Unit | Model | chat log', function (hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('chat-log', {});
assert.ok(model);
});
});