Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as Joi from "joi";
  2. import { ObjectID } from "mongodb";
  3. import JoiPhoneNumber from "../services/joi-phone-number";
  4.  
  5. import { ReceiverBinding } from "../api/graphql/types/receiver-binding";
  6. import { createCollection } from "../services/mongodb";
  7.  
  8. export interface IAttributes {
  9.   identity: string;
  10.   type: ReceiverBinding;
  11.   address: string;
  12. }
  13.  
  14. export interface IInstance extends IAttributes {
  15.   readonly _id: ObjectID;
  16.   readonly updatedAt: Date | null;
  17.   readonly createdAt: Date;
  18. }
  19.  
  20. export const collectionPromise = createCollection<IInstance>({
  21.   name: "ReceiverBindings",
  22. }).then(async collection => {
  23.   await collection.createIndex({ identity: 1, type: 1 }, { unique: true });
  24.   // await collection.createIndex({ identity: 1, type: 1 }, { unique: true }); // remove
  25.   // await collection.createIndex({ identity: 1, type: 1 }, { unique: true }); // remove
  26.   return collection;
  27. });
  28.  
  29. const bindingSchema = Joi.object().keys({
  30.   type: Joi.valid([
  31.     ReceiverBinding.EMAIL,
  32.     ReceiverBinding.IOS_DEVICE_ID,
  33.     ReceiverBinding.PHONE_NUMBER,
  34.     ReceiverBinding.ANDROID_DEVICE_ID,
  35.   ]),
  36.   address: Joi.any()
  37.     .when("type", {
  38.       is: ReceiverBinding.EMAIL,
  39.       then: Joi.string().email(),
  40.     })
  41.     .when("type", {
  42.       is: ReceiverBinding.PHONE_NUMBER,
  43.       then: JoiPhoneNumber.phone().mobile(),
  44.     }),
  45.   identity: Joi.string(),
  46. });
  47.  
  48. export const create = async (binding: IAttributes): Promise<IInstance> => {
  49.   const validationResult = Joi.validate(binding, bindingSchema);
  50.   if (validationResult.error) {
  51.     throw validationResult.error;
  52.   }
  53.  
  54.   const { ops } = await (await collectionPromise).insertOne({
  55.     ...binding,
  56.     createdAt: new Date(),
  57.     updatedAt: null,
  58.   });
  59.   console.log("ops ========>", ops);
  60.   return ops[0];
  61. };
  62.  
  63. export const findAll = async (options: {
  64.   identities?: string[];
  65.   types?: ReceiverBinding[];
  66. }) => {
  67.   const query: {
  68.     types?: { $in: ReceiverBinding[] };
  69.     identities?: { $in: string[] };
  70.   } = {};
  71.  
  72.   if (options.types) {
  73.     query.types = { $in: options.types };
  74.   }
  75.   if (options.identities) {
  76.     query.identities = { $in: options.identities };
  77.   }
  78.  
  79.   return (await collectionPromise)
  80.     .find({
  81.       ...query,
  82.     })
  83.     .toArray();
  84. };
  85.  
  86. export const findOne = async (attributes: {
  87.   identity: string;
  88.   type: ReceiverBinding;
  89. }) =>
  90.   (await collectionPromise).findOne({
  91.     ...attributes,
  92.   });
  93.  
  94. export const update = async (id: ObjectID, updates: Partial<IInstance>) => {
  95.   const { value } = await (await collectionPromise).findOneAndUpdate(
  96.     { _id: id },
  97.     { $set: { updatedAt: new Date(), ...updates } },
  98.     { returnOriginal: false },
  99.   );
  100.  
  101.   if (!value) {
  102.     throw new Error(`ReceiverBinding with id ${id.toHexString()} not found`);
  103.   }
  104.  
  105.   return value;
  106. };
  107.  
  108. export const destroy = async (attributes: {
  109.   identity: string;
  110.   type: ReceiverBinding;
  111. }) => {
  112.   const { deletedCount } = await (await collectionPromise).deleteMany({
  113.     ...attributes,
  114.   });
  115.  
  116.   return {
  117.     deletedCount,
  118.   };
  119. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement