Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
feat(delete): delete a SavedItem by url (#803)
Browse files Browse the repository at this point in the history
* feat(delete): delete a SavedItem by url

[IN-1475]

* restore accidentally deleted mutation in schema

* review fixes

* nit fix
  • Loading branch information
kschelonka authored Jun 2, 2023
1 parent 05dea86 commit cdff4d8
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 22 deletions.
6 changes: 6 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,12 @@ type Mutation {
'Unfavorite' a 'favorite' SavedItem (identified by URL)
"""
savedItemUnFavorite(givenUrl: Url!, timestamp: ISOString!): SavedItem

"""
'Soft-delete' a SavedItem (identified by URL)
"""
savedItemDelete(givenUrl: Url!, timestamp: ISOString!): Url

}

# """
Expand Down
7 changes: 5 additions & 2 deletions src/dataService/savedItemsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ export class SavedItemDataService {
* to allow us to fully rollback should any on of the
* database statements fail.
* @param itemId the itemId to delete
* @param deletedAt optional timestamp for when the mutation was completed;
* defaults to current server time
*/
public async deleteSavedItem(itemId) {
public async deleteSavedItem(itemId, deletedAt?: Date) {
const timestamp = deletedAt ?? SavedItemDataService.formatDate(new Date());
const transaction = await this.db.transaction();
try {
// remove tags for saved item
Expand All @@ -264,7 +267,7 @@ export class SavedItemDataService {
await transaction('list')
.update({
status: SavedItemStatus.DELETED,
time_updated: SavedItemDataService.formatDate(new Date()),
time_updated: timestamp,
api_id_updated: this.apiId,
})
.where({ item_id: itemId, user_id: this.userId });
Expand Down
45 changes: 45 additions & 0 deletions src/models/SavedItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ export class SavedItemModel {
return savedItem;
}

/**
* 'Soft-delete' a Save in a Pocket User's list. Removes tags, scroll
* sync position, and attributions associated with the SavedItem, then
* sets the status to 'deleted'.
* @param id the ID of the SavedItem to delete
* @param timestamp timestamp for when the mutation occurred. Optional
* to support old id-keyed mutations that didn't require timetsamp.
* If not provided, defaults to current server time.
* @returns The ID of the deleted SavedItem, or null if it does not exist
* @throws NotFound if the SavedItem doesn't exist
*/
public async deleteById(
id: string,
timestamp?: Date
): Promise<string | null> {
// TODO: setup a process to delete saved items X number of days after deleted
await this.saveService.deleteSavedItem(id, timestamp);
const savedItem = await this.saveService.getSavedItemById(id);
if (savedItem == null) {
throw new NotFoundError(this.defaultNotFoundMessage);
} else {
this.context.emitItemEvent(EventType.DELETE_ITEM, savedItem);
}
return id;
}

/**
* 'Archive' a Save in a Pocket User's list
* @param url the given url of the SavedItem to archive
Expand Down Expand Up @@ -166,6 +192,25 @@ export class SavedItemModel {
return this.unfavoriteById(id, timestamp);
}

/**
* 'Soft-delete' a Save in a Pocket User's list. Removes tags, scroll
* sync position, and attributions associated with the SavedItem, then
* sets the status to 'deleted'.
* @param id the ID of the SavedItem to delete
* @param timestamp timestamp for when the mutation occurred
* @returns The url of the deleted SavedItem, or null if it does not exist
* @throws NotFound if the SavedItem doesn't exist
*/
public async deleteByUrl(
url: string,
timestamp: Date
): Promise<string | null> {
const id = await this.fetchIdFromUrl(url);
// Will throw if fails or returns null
await this.deleteById(id, timestamp);
return url;
}

/**
* Given a URL, fetch the itemId associated with it from the Parser
* service. This is part of the primary key to identify the savedItem
Expand Down
10 changes: 10 additions & 0 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ const resolvers = {
args.timestamp
);
},
savedItemDelete: async (
_,
args: { givenUrl: string; timestamp: Date },
context: IContext
): Promise<string | null> => {
return await context.models.savedItem.deleteByUrl(
args.givenUrl,
args.timestamp
);
},
},
};

Expand Down
7 changes: 1 addition & 6 deletions src/resolvers/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,7 @@ export async function deleteSavedItem(
args: { id: string },
context: IContext
): Promise<string> {
// TODO: setup a process to delete saved items X number of days after deleted
const savedItemService = new SavedItemDataService(context);
await savedItemService.deleteSavedItem(args.id);
const savedItem = await savedItemService.getSavedItemById(args.id);
context.emitItemEvent(EventType.DELETE_ITEM, savedItem);
return args.id;
return context.models.savedItem.deleteById(args.id);
}

/**
Expand Down
Loading

0 comments on commit cdff4d8

Please sign in to comment.