Skip to content

Commit

Permalink
feat: Add Soft Delete with Recovery and Cleanup Logic for Collections (
Browse files Browse the repository at this point in the history
…#42)

* feat: Add deletedAt column to Collection table for soft delete functionality (#41)

* feat: add deletion logic to set the `deletedAt` timestamp to indicate collection is soft deleted (#45)

* soft delection start, still wip

* fix testing config

* delete weird test

* cleanup files using singleton

* write tests for collection ownership verification utility

* testing config -> TS

* testing documented

* tests fixed, using node instead of jsdom for now

* document verifyCollectionOwnership utility

* remove unnecessary ts path

* feat: Add `recoverCollection` logic (#46)

* organize tests

* adds recover procedure

* document procedure

* fix linting issues

* feat: Create a scheduled task to permanently delete records with deletedAt older than 30 days (#48)

* schedules collections cleanup at 5am each day

* document cleanup process

* feat: Implement "Recently Deleted" Section with Recovery and Permanent Deletion Options (#49)

* can delete & recover collections

* can permanently delete collection

* linting issues
  • Loading branch information
mathewmorris authored Jan 12, 2025
1 parent 761c46a commit 73557ba
Show file tree
Hide file tree
Showing 20 changed files with 1,339 additions and 40 deletions.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,63 @@ The postgres database will be ready for connections at `localhost:5432`.
|`npm run dev:docker`|starts dev environment via docker|
|`npm run dev:logs`|starts reading docker compose log files for application container|

## What happens when I push a new branch to Github?
## Process

### What happens when I push a new branch to Github?
1. Vercel [creates a Preview deployment](https://vercel.com/magicians/magic-vault/deployments)

## What happens when I merge into `main`?
### What happens when I merge into `main`?
1. Vercel [creates a Production deployment](https://vercel.com/magicians/magic-vault/deployments)
2. Database migrations are run automatically with github action `deploy`.

## Document Coding Patterns
> Practicing documenting coding patterns is important. [This video](https://youtu.be/oJbfMBROEO0?si=QL0Xty-Q2nVlaiZo&t=311) speaks on this a little bit

### General rules
- colocate test file (e.g. Component.ts should exist alongside Component.test.ts)
- follow [Arrage-Act-Assert](https://automationpanda.com/2020/07/07/arrange-act-assert-a-pattern-for-writing-good-tests/) when writing tests

### Testing Asynchronous Code
```ts
// Resolving
return expect(
doSomething();
).resolves.toStrictEqual(expectedResult);
// Rejecting
return expect(
doSomething();
).rejects.toStrictEqual(expectedError);
```

## Document Logic

### Collection soft-delete procedure
- When a **collection is active**, it will not have a date in `deletedAt` field
- When user wants to **delete a collection**, `softDelete` procedure will be executed
- When user wants to **recover a collection**, `recoverCollection` procedure will be executed
- Every day, a cronjob will hit the `api/collections/cleanup` endpoint which will execute `destroy30DaysOld`

### `softDelete` procedure
1. If `verifyCollectionOwnership` resolves:
- update collection `deletedAt` field to `new Date()`
2. return collection
### `recoverCollection` procedure
1. If `verifyCollectionOwnership` resolves:
- update collection `deletedAt` field to `null`
2. return collection
### `destroy30DaysOld` procedure
1. If collections exists that have `deletedAt` older than 30 days
- run deleteMany mutation with list of collectionIds
2. return number of deleted collections
### `verifyCollectionOwnership` API utility
- if single collection exists:
- if owner id matches session id:
- resolve with collection
- else:
- reject with FORBIDDEN
- else, reject with NOT_FOUND
17 changes: 0 additions & 17 deletions jest.config.js

This file was deleted.

21 changes: 21 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Config } from 'jest'
import nextJest from 'next/jest.js'

const createJestConfig = nextJest({
dir: './',
})

const config: Config = {
coverageProvider: 'v8',
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>'],
moduleNameMapper: {
"^singleton$": "<rootDir>/singleton.ts"
},
setupFilesAfterEnv: ['<rootDir>/singleton.ts'],
clearMocks: true,
}

export default createJestConfig(config)

Loading

0 comments on commit 73557ba

Please sign in to comment.