-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2064 from RachelDau/issue/2009-notification-added
Issue/2009 Notifications Added
- Loading branch information
Showing
31 changed files
with
2,052 additions
and
16 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
65 changes: 65 additions & 0 deletions
65
packages/app/obojobo-express/__tests__/viewer_notification_state.test.js
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,65 @@ | ||
const db = require('../server/db') | ||
const { | ||
getNotifications, | ||
getRecentNotifications, | ||
setLastLogin | ||
} = require('../server/viewer/viewer_notification_state') | ||
|
||
jest.mock('../server/db') | ||
describe('db', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
jest.resetModules() | ||
}) | ||
|
||
test('returns notifications when passed ids', () => { | ||
const fakeNotifications = [ | ||
{ title: 'Notification 1', text: 'This is notification 1' }, | ||
{ title: 'Notification 2', text: 'This is notification 2' } | ||
] | ||
db.manyOrNone.mockResolvedValue(fakeNotifications) | ||
|
||
return getNotifications([1, 2]).then(result => { | ||
expect(result).toEqual(fakeNotifications) | ||
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { ids: [1, 2] }) | ||
}) | ||
}) | ||
|
||
test('returns undefined when passed ids as 0', () => { | ||
return expect(getNotifications(0)).toBeUndefined() | ||
}) | ||
|
||
test('returns notifications created after a given date', () => { | ||
const fakeNotifications = [{ id: 1 }, { id: 2 }] | ||
db.manyOrNone.mockResolvedValue(fakeNotifications) | ||
|
||
return getRecentNotifications('2022-01-01').then(result => { | ||
expect(result).toEqual(fakeNotifications) | ||
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { date: '2022-01-01' }) | ||
}) | ||
}) | ||
|
||
test('should insert a new record if the user does not exist', () => { | ||
db.none.mockResolvedValue() | ||
|
||
const userId = 1 | ||
const today = '2023-09-13' | ||
|
||
return setLastLogin(userId, today).then(() => { | ||
expect(db.none).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO users'), { | ||
userId, | ||
today | ||
}) | ||
}) | ||
}) | ||
|
||
test('should handle other errors from db.none', () => { | ||
const errorMessage = 'Database error' | ||
db.none.mockRejectedValue(new Error(errorMessage)) | ||
|
||
const userId = 1 | ||
const today = '2023-09-13' | ||
|
||
return expect(setLastLogin(userId, today)).rejects.toThrow(errorMessage) | ||
}) | ||
}) |
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
31 changes: 31 additions & 0 deletions
31
packages/app/obojobo-express/server/migrations/20240506192834-modify-users-add-last-login.js
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,31 @@ | ||
'use strict' | ||
|
||
var dbm | ||
var type | ||
var seed | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function(options, seedLink) { | ||
dbm = options.dbmigrate | ||
type = dbm.dataType | ||
seed = seedLink | ||
} | ||
|
||
exports.up = function(db) { | ||
return db.addColumn('users', 'last_login', { | ||
type: 'timestamp WITH TIME ZONE', | ||
notNull: true, | ||
defaultValue: new String('now()') | ||
}) | ||
} | ||
|
||
exports.down = function(db) { | ||
return db.removeColumn('users', 'last_login') | ||
} | ||
|
||
exports._meta = { | ||
version: 1 | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/app/obojobo-express/server/migrations/20240506193547-create-notification-table.js
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,40 @@ | ||
'use strict' | ||
|
||
var dbm | ||
var type | ||
var seed | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function(options, seedLink) { | ||
dbm = options.dbmigrate | ||
type = dbm.dataType | ||
seed = seedLink | ||
} | ||
|
||
exports.up = function(db) { | ||
return db.createTable('notifications', { | ||
id: { | ||
type: 'bigserial', | ||
primaryKey: true, | ||
notNull: true | ||
}, | ||
created_at: { | ||
type: 'timestamp WITH TIME ZONE', | ||
notNull: true, | ||
defaultValue: new String('now()') | ||
}, | ||
text: { type: 'string', notNull: true }, | ||
title: { type: 'string', notNull: true } | ||
}) | ||
} | ||
|
||
exports.down = function(db) { | ||
return db.dropTable('notifications') | ||
} | ||
|
||
exports._meta = { | ||
version: 1 | ||
} |
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
52 changes: 52 additions & 0 deletions
52
packages/app/obojobo-express/server/viewer/viewer_notification_state.js
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,52 @@ | ||
const db = oboRequire('server/db') | ||
|
||
function getNotifications(ids) { | ||
if (ids !== 0) { | ||
return db.manyOrNone( | ||
` | ||
SELECT title,text | ||
FROM notifications | ||
WHERE id IN ($[ids:csv]) | ||
ORDER BY id ASC | ||
`, | ||
{ | ||
ids | ||
} | ||
) | ||
} | ||
} | ||
|
||
function getRecentNotifications(date) { | ||
return db.manyOrNone( | ||
` | ||
SELECT id | ||
FROM notifications | ||
WHERE created_at >= $[date] | ||
ORDER BY created_at ASC | ||
`, | ||
{ | ||
date | ||
} | ||
) | ||
} | ||
|
||
function setLastLogin(userId, today) { | ||
return db.none( | ||
` | ||
INSERT INTO users (id, last_login) | ||
VALUES ($[userId], $[today]) | ||
ON CONFLICT (id) DO UPDATE | ||
SET last_login = EXCLUDED.last_login | ||
`, | ||
{ | ||
userId, | ||
today | ||
} | ||
) | ||
} | ||
|
||
module.exports = { | ||
getNotifications, | ||
getRecentNotifications, | ||
setLastLogin | ||
} |
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.