Add next_message_number to threads, use it for reply numbers
parent
205262660b
commit
a4c7b84616
|
@ -0,0 +1,11 @@
|
||||||
|
exports.up = async function(knex) {
|
||||||
|
await knex.schema.table("threads", table => {
|
||||||
|
table.integer("next_message_number").defaultTo(1);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.down = async function(knex) {
|
||||||
|
await knex.schema.table("threads", table => {
|
||||||
|
table.dropColumn("next_message_number");
|
||||||
|
});
|
||||||
|
};
|
|
@ -18,6 +18,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS} = require("./constants");
|
||||||
* @property {String} user_id
|
* @property {String} user_id
|
||||||
* @property {String} user_name
|
* @property {String} user_name
|
||||||
* @property {String} channel_id
|
* @property {String} channel_id
|
||||||
|
* @property {Number} next_message_number
|
||||||
* @property {String} scheduled_close_at
|
* @property {String} scheduled_close_at
|
||||||
* @property {String} scheduled_close_id
|
* @property {String} scheduled_close_id
|
||||||
* @property {String} scheduled_close_name
|
* @property {String} scheduled_close_name
|
||||||
|
@ -116,7 +117,7 @@ class Thread {
|
||||||
*/
|
*/
|
||||||
async _addThreadMessageToDB(data) {
|
async _addThreadMessageToDB(data) {
|
||||||
if (data.message_type === THREAD_MESSAGE_TYPE.TO_USER) {
|
if (data.message_type === THREAD_MESSAGE_TYPE.TO_USER) {
|
||||||
data.message_number = knex.raw(`IFNULL((${this._lastMessageNumberInThreadSQL()}), 0) + 1`);
|
data.message_number = await this._getAndIncrementNextMessageNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
const dmChannel = await this.getDMChannel();
|
const dmChannel = await this.getDMChannel();
|
||||||
|
@ -159,15 +160,22 @@ class Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {string}
|
* @returns {Promise<Number>}
|
||||||
* @private
|
|
||||||
*/
|
*/
|
||||||
_lastMessageNumberInThreadSQL() {
|
async _getAndIncrementNextMessageNumber() {
|
||||||
return knex("thread_messages AS tm_msg_num_ref")
|
return knex.transaction(async trx => {
|
||||||
.select(knex.raw("MAX(tm_msg_num_ref.message_number)"))
|
const nextNumberRow = await trx("threads")
|
||||||
.whereRaw(`tm_msg_num_ref.thread_id = '${this.id}'`)
|
.where("id", this.id)
|
||||||
.toSQL()
|
.select("next_message_number")
|
||||||
.sql;
|
.first();
|
||||||
|
const nextNumber = nextNumberRow.next_message_number;
|
||||||
|
|
||||||
|
await trx("threads")
|
||||||
|
.where("id", this.id)
|
||||||
|
.update({ next_message_number: nextNumber + 1 });
|
||||||
|
|
||||||
|
return nextNumber;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue