Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sorting by aliases in favorites #57

Merged
merged 10 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions src/const/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const RETURN_NAVIGATION_COLUMNS = [

export const RETURN_FAVORITES_COLUMNS = [
'favorites.alias',
'favorites.display_alias',
'entries.entryId',
'entries.scope',
'entries.type',
Expand Down
8 changes: 6 additions & 2 deletions src/db/models/favorite/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {raw} from 'objection';
import {Model} from '../..';
import Entry from '../entry';
import {Entry as EntryModel} from '../new/entry';
Expand Down Expand Up @@ -110,7 +111,7 @@ class Favorite extends Model {
.where((builder) => {
if (filters && filters.name) {
builder.where(
'name',
raw('coalesce(favorites.alias, entries.name)'),
'like',
`%${Utils.escapeStringForLike(filters.name.toLowerCase())}%`,
);
Expand Down Expand Up @@ -388,8 +389,11 @@ class Favorite extends Model {

const {login} = requestedBy;

const displayAlias = name ? name : null;
const alias = displayAlias ? displayAlias.toLowerCase() : null;

const result = await Favorite.query(this.primary)
.update({alias: name})
.update({alias, displayAlias})
.where({entryId, tenantId, login})
.returning('*')
.first()
Expand Down
33 changes: 33 additions & 0 deletions src/db/scripts/update-alias-favorites.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable camelcase */
/* This script facilitates migration of aliases for favorites and can be deleted after release */
require('dotenv').config();
require('../../index');
import {db} from '../index';

(async function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to write a description here about reason or use cases for that scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a temporary script which facilitates migration, and will be deleted after release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, added comment with description at top

try {
await db.ready();

const favorites = await db.primary.raw(
`SELECT entry_id, tenant_id, login, alias, display_alias
FROM favorites
WHERE alias IS NOT NULL AND alias != ''`,
);

for (const entry of favorites.rows) {
const {entry_id, tenant_id, login, alias, display_alias} = entry;

if (display_alias === null || alias.toLowerCase() !== display_alias.toLowerCase()) {
await db.primary
.table('favorites')
.update({alias: alias.toLowerCase(), display_alias: alias})
.where({entry_id, tenant_id, login});
}
}

process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}
})();
2 changes: 1 addition & 1 deletion src/types/models/favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface DeleteFavoriteConfig extends BasicRequestParams {
}
export interface RenameFavoriteConfig extends BasicRequestParams {
entryId: string;
name: string;
name: string | null;
ctx: CTX;
dlContext?: string;
}
1 change: 1 addition & 0 deletions src/types/models/tables-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ export interface FavoriteColumns {
tenantId: string;
login: string;
alias: string | null;
displayAlias: string | null;
createdAt: string;
}
2 changes: 1 addition & 1 deletion src/types/services.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export interface AddFavorite extends StdServiceParams {
}
export interface RenameFavorite extends StdServiceParams {
entryId: string;
name: string;
name: string | null;
}
export interface DeleteFavorite extends StdServiceParams {
entryId: string;
Expand Down