Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 39 additions & 21 deletions models/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,32 +496,51 @@ function sanitizeId(value) {
//enrichment over

/**
* Retrieves the name of an activity type from the local SQLite database
* based on the provided Odoo record ID.
* Retrieves the name of an activity type from the local SQLite database.
*
* @function getActivityTypeName
* @param {number} odooRecordId - The ID of the activity type as stored in Odoo.
* @returns {string} - Returns the name of the activity type if found, otherwise an empty string.
* Local Account activity types use the local SQLite id, while
* Odoo account activity types use the odoo_record_id.
*
* @description
* Opens a local SQLite database transaction and queries the `mail_activity_type_app` table
* for a record matching the given `odooRecordId`.
* Extracts the `name` field from the result and returns it.
* Logs any exception encountered during the operation via `DBCommon.logException()`.
* @function getActivityTypeName
* @param {number} activityTypeId - The activity type ID.
* @param {number} accountId - The account ID.
* @returns {string} - Returns the activity type name if found, otherwise an empty string.
*/
function getActivityTypeName(odooRecordId) {
function getActivityTypeName(activityTypeId, accountId) {
if (!activityTypeId || activityTypeId <= 0) {
return "";
}

var typeName = "";

try {
var db = Sql.LocalStorage.openDatabaseSync(DBCommon.NAME, DBCommon.VERSION, DBCommon.DISPLAY_NAME, DBCommon.SIZE);
var db = Sql.LocalStorage.openDatabaseSync(
DBCommon.NAME,
DBCommon.VERSION,
DBCommon.DISPLAY_NAME,
DBCommon.SIZE
);

db.transaction(function (tx) {
var query = `
SELECT name FROM mail_activity_type_app
WHERE odoo_record_id = ?
LIMIT 1
`;
var rs = tx.executeSql(query, [odooRecordId]);
var query = "";
var params = [];

if (accountId === 0) {
query = "SELECT name FROM mail_activity_type_app " +
"WHERE account_id = 0 AND id = ? LIMIT 1";
params = [activityTypeId];
} else if (accountId !== undefined && accountId !== null && accountId > 0) {
query = "SELECT name FROM mail_activity_type_app " +
"WHERE account_id = ? AND odoo_record_id = ? LIMIT 1";
params = [accountId, activityTypeId];
} else {
query = "SELECT name FROM mail_activity_type_app " +
"WHERE (odoo_record_id = ? AND odoo_record_id > 0) " +
"OR id = ? LIMIT 1";
params = [activityTypeId, activityTypeId];
}

var rs = tx.executeSql(query, params);

if (rs.rows.length > 0) {
typeName = rs.rows.item(0).name;
Expand All @@ -534,7 +553,6 @@ function getActivityTypeName(odooRecordId) {

return typeName;
}

/**
* Marks a specific activity record as "done" in the local SQLite database
* by updating its `state` and `status` fields.
Expand Down Expand Up @@ -1848,7 +1866,7 @@ function passesActivitySearchFilter(activity, searchQuery) {
}


var activityTypeName = getActivityTypeName(activity.activity_type_id);
var activityTypeName = getActivityTypeName(activity.activity_type_id, activity.account_id);
if (activityTypeName && activityTypeName.toLowerCase().indexOf(query) >= 0) {
return true;
}
Expand Down Expand Up @@ -2098,4 +2116,4 @@ function getAllDoneActivitiesPaginated(limit, offset) {
}

return activityList;
}
}
31 changes: 31 additions & 0 deletions models/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,37 @@ function ensureDefaultLocalAccountExists() {
}
}

/**
* Ensures default activity types exist for the Local Account.
*
* Local accounts do not sync activity types from Odoo, so provide
* a predefined set of activity types during database initialization.
*/
function ensureDefaultLocalActivityTypes() {
const defaultTypes = ["To Do", "Call", "Email", "Meeting"];

try {
const db = Sql.LocalStorage.openDatabaseSync(NAME, VERSION, DISPLAY_NAME, SIZE);

db.transaction(function (tx) {
defaultTypes.forEach(function (typeName, index) {
const result = tx.executeSql(
"SELECT id FROM mail_activity_type_app WHERE account_id = ? AND name = ? AND (status IS NULL OR status != 'deleted')",
[0, typeName]
);

if (result.rows.length === 0) {
tx.executeSql(
"INSERT INTO mail_activity_type_app (account_id, name, status, odoo_record_id) VALUES (?, ?, ?, ?)",
[0, typeName, "", -(index + 1)]
);
}
});
});
} catch (e) {
logException("ensureDefaultLocalActivityTypes", e);
}
}
/**
* Creates a table if it doesn't exist, and ensures all expected columns are present.
*
Expand Down
2 changes: 2 additions & 0 deletions models/dbinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ function initializeDatabase() {
)',
['id INTEGER', 'account_id INTEGER', 'name TEXT', 'status TEXT DEFAULT ""', 'odoo_record_id INTEGER']
);
// Ensure default activity types for Local Account
DBCommon.ensureDefaultLocalActivityTypes();


DBCommon.createOrUpdateTable("ir_model_app",
Expand Down
2 changes: 1 addition & 1 deletion qml/components/cards/ActivityDetailsCard.qml
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ ListItem {
spacing: units.gu(0.4)

Text {
text: root.activity_type_name || (i18n.dtr("ubtms", "Type ID: ") + root.activity_type_id)
text: root.activity_type_name || i18n.dtr("ubtms", "No Type")
font.pixelSize: units.gu(1.5)
horizontalAlignment: Text.AlignRight
width: units.gu(6)
Expand Down
4 changes: 2 additions & 2 deletions qml/features/activities/pages/Activity_Page.qml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ Page {
summary: item.summary,
due_date: item.due_date,
notes: item.notes,
activity_type_name: Activity.getActivityTypeName(item.activity_type_id),
activity_type_name: Activity.getActivityTypeName(item.activity_type_id, item.account_id),
state: item.state,
task_id: safeTaskId,
task_name: taskName,
Expand Down Expand Up @@ -575,7 +575,7 @@ Page {
}

// Search in activity type name
var activityTypeName = Activity.getActivityTypeName(item.activity_type_id);
var activityTypeName = Activity.getActivityTypeName(item.activity_type_id, item.account_id);
if (activityTypeName && activityTypeName.toLowerCase().indexOf(query) >= 0) {
return true;
}
Expand Down
Loading