Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as Joi from "joi";
- import { ObjectID } from "mongodb";
- import JoiPhoneNumber from "../services/joi-phone-number";
- import { ReceiverBinding } from "../api/graphql/types/receiver-binding";
- import { createCollection } from "../services/mongodb";
- export interface IAttributes {
- identity: string;
- type: ReceiverBinding;
- address: string;
- }
- export interface IInstance extends IAttributes {
- readonly _id: ObjectID;
- readonly updatedAt: Date | null;
- readonly createdAt: Date;
- }
- export const collectionPromise = createCollection<IInstance>({
- name: "ReceiverBindings",
- }).then(async collection => {
- await collection.createIndex({ identity: 1, type: 1 }, { unique: true });
- // await collection.createIndex({ identity: 1, type: 1 }, { unique: true }); // remove
- // await collection.createIndex({ identity: 1, type: 1 }, { unique: true }); // remove
- return collection;
- });
- const bindingSchema = Joi.object().keys({
- type: Joi.valid([
- ReceiverBinding.EMAIL,
- ReceiverBinding.IOS_DEVICE_ID,
- ReceiverBinding.PHONE_NUMBER,
- ReceiverBinding.ANDROID_DEVICE_ID,
- ]),
- address: Joi.any()
- .when("type", {
- is: ReceiverBinding.EMAIL,
- then: Joi.string().email(),
- })
- .when("type", {
- is: ReceiverBinding.PHONE_NUMBER,
- then: JoiPhoneNumber.phone().mobile(),
- }),
- identity: Joi.string(),
- });
- export const create = async (binding: IAttributes): Promise<IInstance> => {
- const validationResult = Joi.validate(binding, bindingSchema);
- if (validationResult.error) {
- throw validationResult.error;
- }
- const { ops } = await (await collectionPromise).insertOne({
- ...binding,
- createdAt: new Date(),
- updatedAt: null,
- });
- console.log("ops ========>", ops);
- return ops[0];
- };
- export const findAll = async (options: {
- identities?: string[];
- types?: ReceiverBinding[];
- }) => {
- const query: {
- types?: { $in: ReceiverBinding[] };
- identities?: { $in: string[] };
- } = {};
- if (options.types) {
- query.types = { $in: options.types };
- }
- if (options.identities) {
- query.identities = { $in: options.identities };
- }
- return (await collectionPromise)
- .find({
- ...query,
- })
- .toArray();
- };
- export const findOne = async (attributes: {
- identity: string;
- type: ReceiverBinding;
- }) =>
- (await collectionPromise).findOne({
- ...attributes,
- });
- export const update = async (id: ObjectID, updates: Partial<IInstance>) => {
- const { value } = await (await collectionPromise).findOneAndUpdate(
- { _id: id },
- { $set: { updatedAt: new Date(), ...updates } },
- { returnOriginal: false },
- );
- if (!value) {
- throw new Error(`ReceiverBinding with id ${id.toHexString()} not found`);
- }
- return value;
- };
- export const destroy = async (attributes: {
- identity: string;
- type: ReceiverBinding;
- }) => {
- const { deletedCount } = await (await collectionPromise).deleteMany({
- ...attributes,
- });
- return {
- deletedCount,
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement