-
Notifications
You must be signed in to change notification settings - Fork 5
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 #535 from JiscSD/OC-732
OC-732: Preparing for scheduled notifications
- Loading branch information
Showing
15 changed files
with
182 additions
and
113 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 was deleted.
Oops, something went wrong.
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,28 @@ | ||
import * as client from '../src/lib/client'; | ||
|
||
// Expects an email address to be supplied to send a notification to. Should be executed like this: | ||
// npm run insertDummyEvent test.example@domain.com | ||
const insertDummyEvent = async (): Promise<string> => { | ||
if (process.argv[2]) { | ||
const to = process.argv[2]; | ||
const data = { | ||
to | ||
}; | ||
await client.prisma.event.create({ | ||
data: { | ||
type: 'dummy', | ||
data | ||
} | ||
}); | ||
|
||
return 'Done'; | ||
} else { | ||
console.log('No to address supplied! Please supply one.'); | ||
|
||
return 'Failed'; | ||
} | ||
}; | ||
|
||
insertDummyEvent() | ||
.then((message) => console.log(message)) | ||
.catch((err) => console.log(err)); |
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
-- CreateTable | ||
CREATE TABLE "Event" ( | ||
"id" TEXT NOT NULL, | ||
"type" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"data" JSONB NOT NULL, | ||
|
||
CONSTRAINT "Event_pkey" PRIMARY KEY ("id") | ||
); |
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as client from 'lib/client'; | ||
import * as testUtils from 'lib/testUtils'; | ||
import * as eventController from 'event/controller'; | ||
|
||
describe('Process events', () => { | ||
beforeAll(async () => { | ||
await testUtils.clearDB(); | ||
}); | ||
|
||
test('Process a dummy event', async () => { | ||
const data = { | ||
to: '[email protected]' | ||
}; | ||
await client.prisma.event.create({ | ||
data: { | ||
type: 'dummy', | ||
data | ||
} | ||
}); | ||
const processEvents = await eventController.dailyEventCheck(); | ||
expect(processEvents.body).toEqual(JSON.stringify({ message: 'Processed 1 event' })); | ||
}); | ||
}); |
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,43 @@ | ||
import * as I from 'interface'; | ||
import * as email from 'email'; | ||
import * as response from 'lib/response'; | ||
import * as eventService from 'event/service'; | ||
import { Prisma } from '@prisma/client'; | ||
|
||
/** | ||
* @todo: Remove code related to dummy events once functionality has been tested. | ||
*/ | ||
export const dailyEventCheck = async (): Promise<I.JSONResponse> => { | ||
// Collect all pending dummy events | ||
const events = await eventService.getByTypes(['dummy']); | ||
|
||
let successCount = 0; | ||
let failureCount = 0; | ||
|
||
// Send an email to an address specified in the event json | ||
for (const dummyEvent of events) { | ||
if (dummyEvent.data) { | ||
const eventData = dummyEvent.data as Prisma.JsonObject; | ||
|
||
if (eventData.to) { | ||
await email.dummyEventNotification(eventData.to as string); | ||
// Delete event record when done | ||
await eventService.deleteEvent(dummyEvent.id); | ||
successCount++; | ||
} else { | ||
console.log('Unable to process event; no to address provided', dummyEvent.id); | ||
failureCount++; | ||
} | ||
} | ||
} | ||
|
||
if (failureCount) { | ||
return response.json(200, { | ||
message: `Finished with ${failureCount} error${failureCount !== 1 ? 's' : ''}` | ||
}); | ||
} | ||
|
||
return response.json(200, { | ||
message: `Processed ${successCount} event${successCount !== 1 ? 's' : ''}` | ||
}); | ||
}; |
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,3 @@ | ||
import * as eventController from 'event/controller'; | ||
|
||
export const dailyEventCheck = eventController.dailyEventCheck; |
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,16 @@ | ||
import * as client from 'lib/client'; | ||
import * as I from 'lib/interface'; | ||
|
||
export const getByTypes = async (types: I.EventType[]) => | ||
client.prisma.event.findMany({ | ||
where: { | ||
type: { | ||
in: types | ||
} | ||
} | ||
}); | ||
|
||
export const deleteEvent = async (id: string) => | ||
client.prisma.event.delete({ | ||
where: { id } | ||
}); |
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