fix sqlite subscription data to return native dates
authorJustin Wind <justin.wind+git@gmail.com>
Mon, 23 Aug 2021 21:51:51 +0000 (14:51 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Mon, 23 Aug 2021 21:51:51 +0000 (14:51 -0700)
This resolves the admin subscription page not displaying, when using
sqlite backend.

src/db/sqlite/index.js
test/src/db/sqlite.js

index a4c3d38fa0f252b94982f5b12832a4bb0815dbb7..9ef1fe54d2302e9523860a6196ebac9cd4f3a323 100644 (file)
@@ -299,12 +299,29 @@ class DatabaseSQLite extends Database {
   }
 
 
+  /**
+   * Converts engine subscription fields to native types.
+   * @param {Object} data
+   */
+  static _subscriptionDataToNative(data) {
+    const epochToDate = (epoch) => new Date(Number(epoch) * 1000);
+    if (data) {
+      ['created', 'verified', 'expires', 'contentDelivered'].forEach((field) => {
+        // eslint-disable-next-line security/detect-object-injection
+        data[field] = epochToDate(data[field]);
+      });
+    }
+    return data;
+  }
+
+
   subscriptionsByTopicId(dbCtx, topicId) {
     const _scope = _fileScope('subscriptionsByTopicId');
     this.logger.debug(_scope, 'called', { topicId });
 
     try {
-      return this.statement.subscriptionsByTopicId.all({ topicId });
+      const subscriptions = this.statement.subscriptionsByTopicId.all({ topicId });
+      return subscriptions.map((s) => DatabaseSQLite._subscriptionDataToNative(s));
     } catch (e) {
       this.logger.error(_scope, 'failed', { error: e, topicId });
       throw e;
@@ -463,7 +480,7 @@ class DatabaseSQLite extends Database {
     let subscription;
     try {
       subscription = this.statement.subscriptionGet.get({ callback, topicId });
-      return subscription;
+      return DatabaseSQLite._subscriptionDataToNative(subscription);
     } catch (e) {
       this.logger.error(_scope, 'failed', { error: e, callback, topicId });
       throw e;
@@ -478,7 +495,7 @@ class DatabaseSQLite extends Database {
     let subscription;
     try {
       subscription = this.statement.subscriptionGetById.get({ subscriptionId });
-      return subscription;
+      return DatabaseSQLite._subscriptionDataToNative(subscription);
     } catch (e) {
       this.logger.error(_scope, 'failed', { error: e, subscriptionId });
       throw e;
index 310e87ad3da7e1203b50de9f2ad3f1d19ed34405..0c96df364d1d4d5ecfb98b6de6e2a021a3c92a05 100644 (file)
@@ -339,7 +339,7 @@ describe('DatabaseSQLite', function () {
 
   describe('subscriptionsByTopicId', function () {
     it('success', async function () {
-      const expected = { count: 3 };
+      const expected = [{ id: 3 }];
       sinon.stub(db.statement.subscriptionsByTopicId, 'all').returns(expected);
       const result = await db.subscriptionsByTopicId(dbCtx, topicUrl);
       assert.deepStrictEqual(result, expected);