Database

Methods

(async, inner) Find(phone, db) → {Promise.<(Account|null)>}

Looks up the phone number in the user account registry.

Parameters:
Name Type Description
phone string

Phone number to be searched

db Mongo.Db

Database to be scoured

Source:
Throws:

If the search operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError
Returns:

An Account object if a match is found. If a match is found but the referrer account has been soft-deleted, returns null. If no match is found, returns null.

Type
Promise.<(Account|null)>

(async, inner) FindAndReplace(account, db) → {Promise.<Account>}

Finds a user account registry entry that matches the given account information and replaces that document with the account document provided.

Parameters:
Name Type Description
account Account

Phone number to be searched

db Mongo.Db

Database to be scoured

Source:
Throws:

If the operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError
Returns:

The updated Account object if a match is found and successfully replaced by the document provided.

Type
Promise.<Account>

(async, inner) GetAccountByID(id, db) → {Promise.<(Account|null)>}

Given a user id, retrieves the full account information stored against that id in the database.

Parameters:
Name Type Description
id string

Magic Batua user ID

db Mongo.Db

Database to be searched

Source:
Throws:

If the search operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError
Returns:

An Account object if a match is found. If a match is found but the referrer account has been soft-deleted, returns null. If no match is found, returns null.

Type
Promise.<(Account|null)>

(async, inner) GetReferrer(inviteCode, db) → {Promise.<(Account|null)>}

When a user is referred to Magic Batua by their friends and family, their signup request contains an inviteCode that helps us identify the referrer in our systems. This is the method that does that heavy lifting.

This method searches the database to find the Account having the same referralCode property as the given inviteCode. If a match is found, it is deemed the rightful referrer and returned to the calling scope.

Parameters:
Name Type Description
inviteCode string

Referral code used at signup

db Mongo.Db

Database to be scoured

Source:
Throws:

If the search operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError
Returns:

An Account object if a referrer is found. If a match is found but the referrer account has been soft-deleted, returns null. If no match is found, returns null.

Type
Promise.<(Account|null)>

(async, inner) Insert(account, db) → {Promise.<Account>}

Inserts the given account document into the MongoDB database instance held by db. No validation checks are run before writing the object to database.

Parameters:
Name Type Description
account Account

Account object to be written to database

db Mongo.Db

The recipient MongoDB database

Source:
Throws:

If the write operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError
Returns:

A fully instantiated Account object

Type
Promise.<Account>

(async, inner) IsDuplicate(account, db) → {Promise.<boolean>}

Checks whether the given account information is already registered with us.

Parameters:
Name Type Description
account Account

Account information

db Mongo.Db

Database to be scoured

Source:
Throws:

If the search operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError
Returns:

true if a matching account is found. If a match is found but the account has been soft-deleted, returns false. If no match is found, returns false.

Type
Promise.<boolean>

(async, inner) UpdateInPlace(id, query, db)

Updates the database object corresponding to the given _id in place. Doesn't run data validation checks, so use very carefully and sparsely. Most of the update operations in the software are done through the Database.FindAndReplace() method. Use that.

Only allows updates for name, email or phone.

If the query parameter contains a Magic Points transaction entry, then the entry is pushed to the pointsLedger.transactions array.

Parameters:
Name Type Description
id string

Magic Batua user _id

query any

A JSON containing key-value pairs that should be updated.

db Mongo.Db

MongoDB database instance

Source:
Throws:

If the operation fails, a 424 Failed Dependency error is thrown.

Type
ExternalError