@flink-app/firebase-messaging-plugin 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/.flink/generatedHandlers.ts +1 -1
  2. package/.flink/generatedJobs.ts +1 -1
  3. package/.flink/generatedRepos.ts +1 -1
  4. package/.flink/schemas/schemas.json +2 -2
  5. package/.flink/schemas/schemas.ts +3 -3
  6. package/.flink/start.ts +1 -1
  7. package/dist/.flink/generatedHandlers.js +1 -1
  8. package/dist/.flink/generatedJobs.js +1 -1
  9. package/dist/.flink/generatedRepos.js +1 -1
  10. package/dist/.flink/schemas/schemas.d.ts +2 -2
  11. package/dist/.flink/schemas/schemas.json +2 -2
  12. package/dist/.flink/start.js +1 -1
  13. package/dist/src/ManagementModule.d.ts +3 -0
  14. package/dist/src/ManagementModule.js +67 -0
  15. package/dist/src/index.d.ts +1 -0
  16. package/dist/src/index.js +1 -0
  17. package/dist/src/managementHandlers/Get.d.ts +4 -0
  18. package/dist/src/managementHandlers/Get.js +66 -0
  19. package/dist/src/managementHandlers/PostMessage.d.ts +4 -0
  20. package/dist/src/managementHandlers/PostMessage.js +83 -0
  21. package/dist/src/schemas/ManagementGet.d.ts +7 -0
  22. package/dist/src/schemas/ManagementGet.js +2 -0
  23. package/dist/src/schemas/ManagementModule.d.ts +28 -0
  24. package/dist/src/schemas/ManagementModule.js +2 -0
  25. package/dist/src/schemas/ManagementPostMessage.d.ts +10 -0
  26. package/dist/src/schemas/ManagementPostMessage.js +2 -0
  27. package/package.json +4 -3
  28. package/readme.md +31 -0
  29. package/src/ManagementModule.ts +50 -0
  30. package/src/handlers/PostMessage.ts +9 -18
  31. package/src/index.ts +2 -0
  32. package/src/managementHandlers/Get.ts +30 -0
  33. package/src/managementHandlers/PostMessage.ts +45 -0
  34. package/src/schemas/ManagementGet.ts +8 -0
  35. package/src/schemas/ManagementModule.ts +33 -0
  36. package/src/schemas/ManagementPostMessage.ts +7 -0
@@ -1,4 +1,4 @@
1
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
1
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
2
2
  import { autoRegisteredHandlers, HttpMethod } from "@flink-app/flink";
3
3
  import * as PostMessage_0 from "../src/handlers/PostMessage";
4
4
 
@@ -1,4 +1,4 @@
1
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
1
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
2
2
  import { autoRegisteredJobs } from "@flink-app/flink";
3
3
  export const jobs = [];
4
4
  autoRegisteredJobs.push(...jobs);
@@ -1,4 +1,4 @@
1
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
1
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
2
2
  import { autoRegisteredRepos } from "@flink-app/flink";
3
3
  export const repos = [];
4
4
  autoRegisteredRepos.push(...repos);
@@ -2,7 +2,7 @@
2
2
  "$schema": "http://json-schema.org/draft-07/schema#",
3
3
  "$ref": "#/definitions/Schemas",
4
4
  "definitions": {
5
- "PostMessage_17_ReqSchema": {
5
+ "PostMessage_12_ReqSchema": {
6
6
  "$schema": "http://json-schema.org/draft-07/schema#",
7
7
  "type": "object",
8
8
  "additionalProperties": false,
@@ -43,7 +43,7 @@
43
43
  ],
44
44
  "definitions": {}
45
45
  },
46
- "PostMessage_17_ResSchema": {
46
+ "PostMessage_12_ResSchema": {
47
47
  "$schema": "http://json-schema.org/draft-07/schema#",
48
48
  "type": "object",
49
49
  "additionalProperties": false,
@@ -1,7 +1,7 @@
1
1
  import Message from "../../src/schemas/Message";
2
2
  import SendResult from "../../src/schemas/SendResult";
3
3
 
4
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
5
- export interface PostMessage_17_ReqSchema extends Message {}
4
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
5
+ export interface PostMessage_12_ReqSchema extends Message {}
6
6
 
7
- export interface PostMessage_17_ResSchema extends SendResult {}
7
+ export interface PostMessage_12_ResSchema extends SendResult {}
package/.flink/start.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
1
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
2
2
  import "./generatedHandlers";
3
3
  import "./generatedRepos";
4
4
  import "./generatedJobs";
@@ -20,7 +20,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
22
  exports.handlers = void 0;
23
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
23
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
24
24
  var flink_1 = require("@flink-app/flink");
25
25
  var PostMessage_0 = __importStar(require("../src/handlers/PostMessage"));
26
26
  exports.handlers = [{ handler: PostMessage_0, assumedHttpMethod: flink_1.HttpMethod.post }];
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jobs = void 0;
4
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
4
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
5
5
  var flink_1 = require("@flink-app/flink");
6
6
  exports.jobs = [];
7
7
  flink_1.autoRegisteredJobs.push.apply(flink_1.autoRegisteredJobs, exports.jobs);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.repos = void 0;
4
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
4
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
5
5
  var flink_1 = require("@flink-app/flink");
6
6
  exports.repos = [];
7
7
  flink_1.autoRegisteredRepos.push.apply(flink_1.autoRegisteredRepos, exports.repos);
@@ -1,6 +1,6 @@
1
1
  import Message from "../../src/schemas/Message";
2
2
  import SendResult from "../../src/schemas/SendResult";
3
- export interface PostMessage_17_ReqSchema extends Message {
3
+ export interface PostMessage_12_ReqSchema extends Message {
4
4
  }
5
- export interface PostMessage_17_ResSchema extends SendResult {
5
+ export interface PostMessage_12_ResSchema extends SendResult {
6
6
  }
@@ -2,7 +2,7 @@
2
2
  "$schema": "http://json-schema.org/draft-07/schema#",
3
3
  "$ref": "#/definitions/Schemas",
4
4
  "definitions": {
5
- "PostMessage_17_ReqSchema": {
5
+ "PostMessage_12_ReqSchema": {
6
6
  "$schema": "http://json-schema.org/draft-07/schema#",
7
7
  "type": "object",
8
8
  "additionalProperties": false,
@@ -43,7 +43,7 @@
43
43
  ],
44
44
  "definitions": {}
45
45
  },
46
- "PostMessage_17_ResSchema": {
46
+ "PostMessage_12_ResSchema": {
47
47
  "$schema": "http://json-schema.org/draft-07/schema#",
48
48
  "type": "object",
49
49
  "additionalProperties": false,
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- // Generated Sat Mar 25 2023 15:16:30 GMT+0100 (Central European Standard Time)
3
+ // Generated Tue May 09 2023 09:38:38 GMT+0200 (Central European Summer Time)
4
4
  require("./generatedHandlers");
5
5
  require("./generatedRepos");
6
6
  require("./generatedJobs");
@@ -0,0 +1,3 @@
1
+ import { ManagementApiModule } from "@flink-app/management-api-plugin";
2
+ import { GetManagementModuleConfig } from "./schemas/ManagementModule";
3
+ export declare const GetManagementModule: (config: GetManagementModuleConfig) => ManagementApiModule;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.GetManagementModule = void 0;
23
+ var flink_1 = require("@flink-app/flink");
24
+ var management_api_plugin_1 = require("@flink-app/management-api-plugin");
25
+ var getManagementPluginHandler = __importStar(require("./managementHandlers/Get"));
26
+ var postMessageHandler = __importStar(require("./managementHandlers/PostMessage"));
27
+ var GetManagementModule = function (config) {
28
+ var _a;
29
+ if (config.pluginId == null)
30
+ config.pluginId = "managementNotificationsApi";
31
+ var endpoints = [];
32
+ endpoints.push({
33
+ routeProps: {
34
+ path: "",
35
+ method: flink_1.HttpMethod.get,
36
+ origin: config.pluginId,
37
+ },
38
+ handler: getManagementPluginHandler,
39
+ });
40
+ endpoints.push({
41
+ routeProps: {
42
+ path: "/message",
43
+ method: flink_1.HttpMethod.post,
44
+ origin: config.pluginId,
45
+ },
46
+ handler: postMessageHandler,
47
+ });
48
+ var features = [];
49
+ var module = {
50
+ id: config.pluginId || "notifications",
51
+ uiSettings: {
52
+ title: ((_a = config.uiSettings) === null || _a === void 0 ? void 0 : _a.title) || "Notifications",
53
+ icon: "",
54
+ features: features,
55
+ },
56
+ ui: config.ui,
57
+ type: management_api_plugin_1.ManagementApiType.notification,
58
+ endpoints: endpoints,
59
+ data: {
60
+ segments: config.segments,
61
+ data: config.data,
62
+ callback: config.messageSentCallback,
63
+ },
64
+ };
65
+ return module;
66
+ };
67
+ exports.GetManagementModule = GetManagementModule;
@@ -1,2 +1,3 @@
1
1
  export * from "./firebaseMessagingPlugin";
2
2
  export * from "./FirebaseMessagingContext";
3
+ export * from "./ManagementModule";
package/dist/src/index.js CHANGED
@@ -12,3 +12,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./firebaseMessagingPlugin"), exports);
14
14
  __exportStar(require("./FirebaseMessagingContext"), exports);
15
+ __exportStar(require("./ManagementModule"), exports);
@@ -0,0 +1,4 @@
1
+ import { FlinkContext, Handler } from "@flink-app/flink";
2
+ import { ManagementGetRequest, ManagementGetResponse } from "../schemas/ManagementGet";
3
+ declare const getManagementPluginHandler: Handler<FlinkContext, ManagementGetRequest, ManagementGetResponse>;
4
+ export default getManagementPluginHandler;
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var getManagementPluginHandler = function (_a) {
40
+ var ctx = _a.ctx, req = _a.req, origin = _a.origin;
41
+ return __awaiter(void 0, void 0, void 0, function () {
42
+ var modules, module, segments, messageingData, data;
43
+ var _b, _c;
44
+ return __generator(this, function (_d) {
45
+ modules = ctx.plugins.managementApi.moduleList.modules;
46
+ module = modules.find(function (p) { return p.id == origin; });
47
+ segments = (_b = module.data) === null || _b === void 0 ? void 0 : _b.segments;
48
+ messageingData = ((_c = module.data) === null || _c === void 0 ? void 0 : _c.data) || [];
49
+ data = {
50
+ segments: segments.map(function (s) {
51
+ var sv = {
52
+ id: s.id,
53
+ description: s.description,
54
+ };
55
+ return sv;
56
+ }),
57
+ data: messageingData,
58
+ };
59
+ return [2 /*return*/, {
60
+ data: data,
61
+ status: 200,
62
+ }];
63
+ });
64
+ });
65
+ };
66
+ exports.default = getManagementPluginHandler;
@@ -0,0 +1,4 @@
1
+ import { FlinkContext, Handler } from "@flink-app/flink";
2
+ import { ManagementPostMessageRequest, ManagementPostMessageResponse } from "../schemas/ManagementPostMessage";
3
+ declare const postMessageHandler: Handler<FlinkContext, ManagementPostMessageRequest, ManagementPostMessageResponse>;
4
+ export default postMessageHandler;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var postMessageHandler = function (_a) {
40
+ var ctx = _a.ctx, req = _a.req, origin = _a.origin;
41
+ return __awaiter(void 0, void 0, void 0, function () {
42
+ var modules, module, segment, targets, tokens, message, callback;
43
+ var _b, _c, _d;
44
+ return __generator(this, function (_e) {
45
+ switch (_e.label) {
46
+ case 0:
47
+ modules = ctx.plugins.managementApi.moduleList.modules;
48
+ module = modules.find(function (p) { return p.id == origin; });
49
+ segment = (_c = (_b = module === null || module === void 0 ? void 0 : module.data) === null || _b === void 0 ? void 0 : _b.segments) === null || _c === void 0 ? void 0 : _c.find(function (p) { return p.id === req.body.segment; });
50
+ if (!segment) {
51
+ return [2 /*return*/, {
52
+ data: { message: "Segment not found" },
53
+ status: 500,
54
+ }];
55
+ }
56
+ return [4 /*yield*/, segment.handler(ctx)];
57
+ case 1:
58
+ targets = _e.sent();
59
+ tokens = targets.map(function (t) { return t.pushToken; }).flat();
60
+ message = {
61
+ to: tokens,
62
+ notification: {
63
+ title: req.body.subject,
64
+ body: req.body.body,
65
+ },
66
+ data: req.body.data,
67
+ };
68
+ return [4 /*yield*/, ctx.plugins["firebaseMessaging"].send(message)];
69
+ case 2:
70
+ _e.sent();
71
+ callback = (_d = module === null || module === void 0 ? void 0 : module.data) === null || _d === void 0 ? void 0 : _d.callback;
72
+ if (callback) {
73
+ callback(ctx, targets, message);
74
+ }
75
+ return [2 /*return*/, {
76
+ data: {},
77
+ status: 200,
78
+ }];
79
+ }
80
+ });
81
+ });
82
+ };
83
+ exports.default = postMessageHandler;
@@ -0,0 +1,7 @@
1
+ import { MessagingData, MessagingSegmentView } from "./ManagementModule";
2
+ export interface ManagementGetRequest {
3
+ }
4
+ export interface ManagementGetResponse {
5
+ segments: MessagingSegmentView[];
6
+ data: MessagingData[];
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,28 @@
1
+ import { FlinkContext } from "@flink-app/flink";
2
+ import Message from "./Message";
3
+ export interface MessagingSegment {
4
+ id: string;
5
+ description: string;
6
+ handler(ctx: FlinkContext<any>): Promise<MessagingTarget[]>;
7
+ }
8
+ export interface MessagingSegmentView extends Omit<MessagingSegment, "handler"> {
9
+ }
10
+ export interface MessagingTarget {
11
+ userId: string;
12
+ pushToken: string[];
13
+ }
14
+ export interface MessagingData {
15
+ id: string;
16
+ description: string;
17
+ options?: string[];
18
+ }
19
+ export interface GetManagementModuleConfig {
20
+ pluginId?: string;
21
+ ui: boolean;
22
+ uiSettings?: {
23
+ title: string;
24
+ };
25
+ segments: MessagingSegment[];
26
+ data?: MessagingData[];
27
+ messageSentCallback?: (ctx: any, target: MessagingTarget[], message: Message) => void;
28
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ export interface ManagementPostMessageRequest {
2
+ segment: string;
3
+ subject: string;
4
+ body: string;
5
+ data: {
6
+ [key: string]: string;
7
+ };
8
+ }
9
+ export interface ManagementPostMessageResponse {
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/firebase-messaging-plugin",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Flink plugin to send Firebase cloud messages",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\"",
@@ -16,10 +16,11 @@
16
16
  "access": "public"
17
17
  },
18
18
  "dependencies": {
19
+ "@flink-app/management-api-plugin": "^0.6.0",
19
20
  "fcm-push": "^1.1.3"
20
21
  },
21
22
  "devDependencies": {
22
- "@flink-app/flink": "^0.5.0",
23
+ "@flink-app/flink": "^0.6.0",
23
24
  "@types/express": "^4.17.12",
24
25
  "@types/node": "^15.6.2",
25
26
  "nodemon": "^2.0.7",
@@ -27,5 +28,5 @@
27
28
  "tsc-watch": "^4.2.9",
28
29
  "typescript": "^4.2.4"
29
30
  },
30
- "gitHead": "3c877c210da19119c074c7482e97a485d2334e80"
31
+ "gitHead": "42ab7a99632d1fba08096d51800dfc0deb4b0222"
31
32
  }
package/readme.md CHANGED
@@ -44,3 +44,34 @@ export interface Ctx extends FlinkContext<FirebaseMessagingContext> {
44
44
  ## Configuration
45
45
 
46
46
  - `serverKey` - The firebase server key
47
+
48
+
49
+ ## Use as a managementmodule in the management-api-plugin
50
+
51
+ Initiate the module and configure it:
52
+
53
+ ```
54
+ import { GetManagementModule as GetNotificationManagementModule } from "@flink-app/firebase-messaging-plugin"
55
+ const notificationManagementModule = GetNotificationManagementModule({
56
+ ui: true,
57
+ uiSettings: {
58
+ title: "Notifications"
59
+ },
60
+ segments : [{
61
+ id : "all",
62
+ description : "All app users",
63
+ handler : async (ctx : Ctx) => {
64
+ const users = await ctx.repos.userRepo.findAll({})
65
+ return users.map(u=>({
66
+ userId : u._id.toString(),
67
+ pushToken : u.pushNotificationTokens.map(p=>p.token)
68
+ }))
69
+ },
70
+
71
+ }],
72
+ data : [],
73
+ })
74
+ ```
75
+
76
+
77
+
@@ -0,0 +1,50 @@
1
+ import { HttpMethod } from "@flink-app/flink";
2
+ import { ManagementApiModule, ManagementApiType } from "@flink-app/management-api-plugin";
3
+ import * as getManagementPluginHandler from "./managementHandlers/Get";
4
+ import * as postMessageHandler from "./managementHandlers/PostMessage";
5
+ import { GetManagementModuleConfig } from "./schemas/ManagementModule";
6
+
7
+ export const GetManagementModule = (config: GetManagementModuleConfig): ManagementApiModule => {
8
+ if (config.pluginId == null) config.pluginId = "managementNotificationsApi";
9
+
10
+ let endpoints: ManagementApiModule["endpoints"] = [];
11
+
12
+ endpoints.push({
13
+ routeProps: {
14
+ path: "",
15
+ method: HttpMethod.get,
16
+ origin: config.pluginId,
17
+ },
18
+ handler: getManagementPluginHandler,
19
+ });
20
+
21
+ endpoints.push({
22
+ routeProps: {
23
+ path: "/message",
24
+ method: HttpMethod.post,
25
+ origin: config.pluginId,
26
+ },
27
+ handler: postMessageHandler,
28
+ });
29
+
30
+ let features: string[] = [];
31
+
32
+ let module: ManagementApiModule = {
33
+ id: config.pluginId || "notifications",
34
+ uiSettings: {
35
+ title: config.uiSettings?.title || "Notifications",
36
+ icon: "",
37
+ features,
38
+ },
39
+ ui: config.ui,
40
+ type: ManagementApiType.notification,
41
+ endpoints: endpoints,
42
+ data: {
43
+ segments: config.segments,
44
+ data: config.data,
45
+ callback: config.messageSentCallback,
46
+ },
47
+ };
48
+
49
+ return module;
50
+ };
@@ -1,29 +1,20 @@
1
- import {
2
- FlinkContext,
3
- Handler,
4
- HttpMethod,
5
- RouteProps,
6
- } from "@flink-app/flink";
1
+ import { FlinkContext, Handler, HttpMethod, RouteProps } from "@flink-app/flink";
7
2
  import { FirebaseMessagingContext } from "../FirebaseMessagingContext";
8
3
  import Message from "../schemas/Message";
9
4
  import SendResult from "../schemas/SendResult";
10
5
 
11
6
  export const Route: RouteProps = {
12
- method: HttpMethod.post,
13
- path: "/send-message",
14
- docs: "Publishes push notification to one or multiple devices",
7
+ method: HttpMethod.post,
8
+ path: "/send-message",
9
+ docs: "Publishes push notification to one or multiple devices",
15
10
  };
16
11
 
17
- const PostMessage: Handler<
18
- FlinkContext<FirebaseMessagingContext>,
19
- Message,
20
- SendResult
21
- > = async ({ ctx, req }) => {
22
- await ctx.plugins.firebaseMessaging.send(req.body);
12
+ const PostMessage: Handler<FlinkContext<FirebaseMessagingContext>, Message, SendResult> = async ({ ctx, req }) => {
13
+ await ctx.plugins.firebaseMessaging.send(req.body);
23
14
 
24
- return {
25
- data: { failedDevices: [] }, // TODO
26
- };
15
+ return {
16
+ data: { failedDevices: [] }, // TODO
17
+ };
27
18
  };
28
19
 
29
20
  export default PostMessage;
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from "./firebaseMessagingPlugin";
2
2
  export * from "./FirebaseMessagingContext";
3
+ export * from "./ManagementModule";
4
+
@@ -0,0 +1,30 @@
1
+ import { FlinkContext, Handler } from "@flink-app/flink";
2
+
3
+ import { ManagementGetRequest, ManagementGetResponse } from "../schemas/ManagementGet";
4
+ import { MessagingData, MessagingSegment, MessagingSegmentView } from "../schemas/ManagementModule";
5
+
6
+ const getManagementPluginHandler: Handler<FlinkContext, ManagementGetRequest, ManagementGetResponse> = async ({ ctx, req, origin }) => {
7
+ const modules = ctx.plugins.managementApi.moduleList.modules as any[];
8
+ const module = modules.find((p) => p.id == origin);
9
+
10
+ const segments = module.data?.segments as MessagingSegment[];
11
+ const messageingData = (module.data?.data as MessagingData[] | undefined) || [];
12
+
13
+ const data = {
14
+ segments: segments.map((s) => {
15
+ let sv: MessagingSegmentView = {
16
+ id: s.id,
17
+ description: s.description,
18
+ };
19
+ return sv;
20
+ }),
21
+ data: messageingData,
22
+ };
23
+
24
+ return {
25
+ data,
26
+ status: 200,
27
+ };
28
+ };
29
+
30
+ export default getManagementPluginHandler;
@@ -0,0 +1,45 @@
1
+ import { FlinkContext, Handler } from "@flink-app/flink";
2
+
3
+ import { MessagingSegment, MessagingTarget } from "../schemas/ManagementModule";
4
+ import { ManagementPostMessageRequest, ManagementPostMessageResponse } from "../schemas/ManagementPostMessage";
5
+ import Message from "../schemas/Message";
6
+
7
+ const postMessageHandler: Handler<FlinkContext, ManagementPostMessageRequest, ManagementPostMessageResponse> = async ({ ctx, req, origin }) => {
8
+ const modules = ctx.plugins.managementApi.moduleList.modules as any[];
9
+ const module = modules.find((p) => p.id == origin);
10
+
11
+ let segment = module?.data?.segments?.find((p: any) => p.id === req.body.segment) as MessagingSegment;
12
+ if (!segment) {
13
+ return {
14
+ data: { message: "Segment not found" },
15
+ status: 500,
16
+ };
17
+ }
18
+
19
+ let targets = await segment.handler(ctx);
20
+
21
+ let tokens = targets.map((t) => t.pushToken).flat();
22
+
23
+ let message: Message = {
24
+ to: tokens,
25
+ notification: {
26
+ title: req.body.subject,
27
+ body: req.body.body,
28
+ },
29
+ data: req.body.data,
30
+ };
31
+
32
+ await ctx.plugins["firebaseMessaging"].send(message);
33
+
34
+ let callback = module?.data?.callback as (ctx: FlinkContext, target: MessagingTarget[], message: Message) => void;
35
+ if (callback) {
36
+ callback(ctx, targets, message);
37
+ }
38
+
39
+ return {
40
+ data: {},
41
+ status: 200,
42
+ };
43
+ };
44
+
45
+ export default postMessageHandler;
@@ -0,0 +1,8 @@
1
+ import { MessagingData, MessagingSegmentView } from "./ManagementModule";
2
+
3
+ export interface ManagementGetRequest {}
4
+
5
+ export interface ManagementGetResponse {
6
+ segments: MessagingSegmentView[];
7
+ data: MessagingData[];
8
+ }
@@ -0,0 +1,33 @@
1
+ import { FlinkContext } from "@flink-app/flink";
2
+ import Message from "./Message";
3
+
4
+ export interface MessagingSegment{
5
+ id : string,
6
+ description : string,
7
+ handler(ctx: FlinkContext<any>) : Promise<MessagingTarget[]>
8
+ }
9
+
10
+
11
+ export interface MessagingSegmentView extends Omit<MessagingSegment, "handler">{
12
+ }
13
+
14
+ export interface MessagingTarget{
15
+ userId : string,
16
+ pushToken : string[]
17
+ }
18
+
19
+ export interface MessagingData{
20
+ id : string,
21
+ description : string,
22
+ options? : string[]
23
+ }
24
+ export interface GetManagementModuleConfig {
25
+ pluginId?: string;
26
+ ui: boolean;
27
+ uiSettings?: {
28
+ title: string;
29
+ };
30
+ segments : MessagingSegment[]
31
+ data? : MessagingData[]
32
+ messageSentCallback? : (ctx : any, target : MessagingTarget[], message : Message) => void
33
+ }
@@ -0,0 +1,7 @@
1
+ export interface ManagementPostMessageRequest {
2
+ segment: string;
3
+ subject: string;
4
+ body: string;
5
+ data: { [key: string]: string };
6
+ }
7
+ export interface ManagementPostMessageResponse {}