generated from well-known-components/template-server
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add lists updated at column (#138)
* feat: Add Update List endpoint * fix: If the name or description are not set avoid trying to update them * test: Test cases in which access utils should not throw errors * test: Add test to check if the update list is being called with the correct params from the handler * test: Fix tests after merging master * refactor: Move queries to a new file called queries instead of utils * fix: Remove util import for debugging purposes * chore: Uninstall lodash * fix: Manually append updatable columns to the update list query * feat: Add migration * feat: Add validations for name and description in the create list handler * feat: When creating a new list, add the value of the description if coming from the UI * feat: Add length validations and generalize create and update handler validations * feat: Create lists support private property * feat: Add schema validator component * feat: Use schema validator for list creation * feat: Use schema validator for list updates * test: Fix & add tests for the new transaction mechanism * feat: Add withTransaction fn to the pg component to generalize the bg, cm, rb, and release behaviors * refactor: Allow async operations in the withTransaction onError parameter * test: Remove assertions related to the new withTransaction fn * test: Test the extension of the pg component * refactor: Use the new implementation of the withTransaction for updating list * refactor: Use the new withTransaction implementation for the addPickToList mechanism * refactor: It's not necesary to check for the AccessNotFound error when creating a new list * refactor: Avoid raising unneeded errors related to the acl when updating a list * feat: Alter lists adding a new column updated_at * feat: Return the updated at prop when getting a list * feat: Allow users to sort lists by updated at * feat: If the list to retrieve is the default use as the updated_at the last pick date * feat: Add triggers to update the updated at when inserting or deleting picks or permissions * fix: Fix failing tests applying the last PR changes to the updated at sorting * Update test/unit/lists-component.spec.ts Co-authored-by: Lautaro Petaccio <1120791+LautaroPetaccio@users.noreply.github.com> Signed-off-by: Kevin Szuchet <31735779+kevinszuchet@users.noreply.github.com> --------- Signed-off-by: Kevin Szuchet <31735779+kevinszuchet@users.noreply.github.com> Co-authored-by: Lautaro Petaccio <1120791+LautaroPetaccio@users.noreply.github.com>
- Loading branch information
1 parent
c2aac50
commit 89641c6
Showing
12 changed files
with
204 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/migrations/1685096522768_add-column-updated-at-to-lists.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate' | ||
import { LISTS_TABLE } from './1677778846950_lists-and-picks' | ||
|
||
export const shorthands: ColumnDefinitions | undefined = undefined | ||
|
||
const FUNCTION_NAME = 'update_lists_updated_at' | ||
const TRIGGER_NAME = 'trigger_update_lists_updated_at' | ||
|
||
export async function up(pgm: MigrationBuilder): Promise<void> { | ||
pgm.addColumns(LISTS_TABLE, { | ||
updated_at: { type: 'timestamp', default: null } | ||
}) | ||
pgm.createIndex(LISTS_TABLE, 'updated_at') | ||
pgm.createFunction( | ||
FUNCTION_NAME, | ||
[], | ||
{ | ||
returns: 'TRIGGER', | ||
language: 'plpgsql', | ||
replace: true | ||
}, | ||
`BEGIN | ||
NEW.updated_at := NOW(); | ||
RETURN NEW; | ||
END;` | ||
) | ||
pgm.createTrigger(LISTS_TABLE, TRIGGER_NAME, { | ||
when: 'BEFORE', | ||
operation: 'UPDATE', | ||
function: FUNCTION_NAME, | ||
level: 'ROW' | ||
}) | ||
} | ||
|
||
export async function down(pgm: MigrationBuilder): Promise<void> { | ||
pgm.dropTrigger(LISTS_TABLE, TRIGGER_NAME) | ||
pgm.dropFunction(FUNCTION_NAME, []) | ||
pgm.dropIndex(LISTS_TABLE, 'updated_at') | ||
pgm.dropColumn(LISTS_TABLE, 'updated_at') | ||
} |
69 changes: 69 additions & 0 deletions
69
src/migrations/1685107172757_add-triggers-to-keep-track-of-lists-updates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate' | ||
import { PICKS_TABLE } from './1677778846950_lists-and-picks' | ||
import { ACL_TABLE } from './1683320488882_acl' | ||
|
||
export const shorthands: ColumnDefinitions | undefined = undefined | ||
|
||
const ON_INSERT_FUNCTION_NAME = 'update_lists_updated_at_when_inserting_related_rows' | ||
const ON_DELETE_FUNCTION_NAME = 'update_lists_updated_at_when_deleting_related_rows' | ||
|
||
export async function up(pgm: MigrationBuilder): Promise<void> { | ||
pgm.createFunction( | ||
ON_INSERT_FUNCTION_NAME, | ||
[], | ||
{ | ||
returns: 'TRIGGER', | ||
language: 'plpgsql', | ||
replace: true | ||
}, | ||
`BEGIN | ||
UPDATE favorites.lists | ||
SET updated_at = NOW() | ||
WHERE id = NEW.list_id; | ||
RETURN NEW; | ||
END;` | ||
) | ||
|
||
pgm.createFunction( | ||
ON_DELETE_FUNCTION_NAME, | ||
[], | ||
{ | ||
returns: 'TRIGGER', | ||
language: 'plpgsql', | ||
replace: true | ||
}, | ||
`BEGIN | ||
UPDATE favorites.lists | ||
SET updated_at = NOW() | ||
WHERE id = OLD.list_id; | ||
RETURN NULL; | ||
END;` | ||
) | ||
|
||
const tables = [PICKS_TABLE, ACL_TABLE] | ||
const functions = [ | ||
{ operation: 'INSERT', name: ON_INSERT_FUNCTION_NAME }, | ||
{ operation: 'DELETE', name: ON_DELETE_FUNCTION_NAME } | ||
] | ||
|
||
tables.forEach(tableName => { | ||
functions.forEach(({ operation, name: functionName }) => { | ||
pgm.createTrigger(tableName, `trigger_update_lists_updated_at_on_${operation.toLowerCase()}`, { | ||
when: 'AFTER', | ||
operation, | ||
function: functionName, | ||
level: 'ROW' | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
export async function down(pgm: MigrationBuilder): Promise<void> { | ||
pgm.dropTrigger(PICKS_TABLE, 'trigger_update_lists_updated_at_on_insert') | ||
pgm.dropTrigger(PICKS_TABLE, 'trigger_update_lists_updated_at_on_delete') | ||
pgm.dropTrigger(ACL_TABLE, 'trigger_update_lists_updated_at_on_insert') | ||
pgm.dropTrigger(ACL_TABLE, 'trigger_update_lists_updated_at_on_delete') | ||
pgm.dropFunction(ON_INSERT_FUNCTION_NAME, []) | ||
pgm.dropFunction(ON_DELETE_FUNCTION_NAME, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.