@magic.batua/account
Version:
The Account modules powers the user account management features of the Magic Batua platform.
1,528 lines (441 loc) • 23.4 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Registry - Postman Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Account.html">Account</a><ul class='methods'><li data-type='method'><a href="Account.html#AddPoints">AddPoints</a></li><li data-type='method'><a href="Account.html#AddReferral">AddReferral</a></li><li data-type='method'><a href="Account.html#CanAuthenticateUsing">CanAuthenticateUsing</a></li><li data-type='method'><a href="Account.html#Delete">Delete</a></li><li data-type='method'><a href="Account.html#Export">Export</a></li><li data-type='method'><a href="Account.html#RedeemPoints">RedeemPoints</a></li><li data-type='method'><a href="Account.html#RemoveReferral">RemoveReferral</a></li><li data-type='method'><a href="Account.html#ResetPassword">ResetPassword</a></li><li data-type='method'><a href="Account.html#SetOTP">SetOTP</a></li><li data-type='method'><a href="Account.html#SetReferrer">SetReferrer</a></li><li data-type='method'><a href="Account.html#Undelete">Undelete</a></li><li data-type='method'><a href="Account.html#UnsetOTP">UnsetOTP</a></li></ul></li><li><a href="Registry.html">Registry</a><ul class='methods'><li data-type='method'><a href="Registry.html#Create">Create</a></li><li data-type='method'><a href="Registry.html#DidResetPassword">DidResetPassword</a></li><li data-type='method'><a href="Registry.html#DidSendOTP">DidSendOTP</a></li><li data-type='method'><a href="Registry.html#HasVerified">HasVerified</a></li><li data-type='method'><a href="Registry.html#IsDuplicate">IsDuplicate</a></li><li data-type='method'><a href="Registry.html#Modify">Modify</a></li><li data-type='method'><a href="Registry.html#Remove">Remove</a></li><li data-type='method'><a href="Registry.html#Retrieve">Retrieve</a></li></ul></li></ul><h3>Modules</h3><ul><li><a href="module-Database.html">Database</a><ul class='methods'><li data-type='method'><a href="module-Database.html#~Find">Find</a></li><li data-type='method'><a href="module-Database.html#~FindAndReplace">FindAndReplace</a></li><li data-type='method'><a href="module-Database.html#~GetAccountByID">GetAccountByID</a></li><li data-type='method'><a href="module-Database.html#~GetReferrer">GetReferrer</a></li><li data-type='method'><a href="module-Database.html#~Insert">Insert</a></li><li data-type='method'><a href="module-Database.html#~IsDuplicate">IsDuplicate</a></li><li data-type='method'><a href="module-Database.html#~UpdateInPlace">UpdateInPlace</a></li></ul></li><li><a href="module-Source.html">Source</a></li></ul>
</nav>
<div id="main">
<h1 class="page-title">Registry</h1>
<section>
<header>
<h2>
Registry
</h2>
<div class="class-description"><p>The <code>Registry</code> class provides the interface between the API server and the <code>Account</code>
module. This class defines the methods and interfaces responsible for new account
registration, login authentication, profile updates and account deletion/undeletion
requests.</p>
<ul>
<li>See <a href="Registry.html#Create"><code>Create()</code></a> to understand signup logic.</li>
<li>See <a href="Registry.html#Retrieve"><code>Retrieve()</code></a> to understand login logic.</li>
<li>See <a href="Registry.html#Modify"><code>Modify()</code></a> to understand update profile logic.</li>
<li>See <a href="Registry.html#Remove"><code>Remove()</code></a> to understand account deletion logic.</li>
</ul>
<p> In addition to these functionalities, the <code>Registry</code> class also defines several utility
methods that carry out key responsibilities. <a href="Registry.html#DidSendOTP"><code>DidSendOTP()</code></a>
and <a href="Registry.html#HasVerified"><code>HasVerified()</code></a> ensure smooth OTP-based verification.
While <a href="Registry.html#DidResetPassword"><code>DidResetPassword()</code></a> carries out the seemingly
straightforward but actually quite complex task of resetting a user's password.</p></div>
</header>
<article>
<div class="container-overview">
<h2>Constructor</h2>
<h4 class="name" id="Registry"><span class="type-signature"></span>new Registry<span class="signature">(db, messaging)</span><span class="type-signature"></span></h4>
<div class="description">
<p>A <code>Registry</code> instance needs access to the messaging API and database for proper
functioning. This constructor initialises an instance with fully-configured
<code>Messaging</code> and <code>Mongo.Db</code> instances.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>db</code></td>
<td class="type">
<span class="param-type">Mongo.Db</span>
</td>
<td class="description last"><p>A MongoDB database instance</p></td>
</tr>
<tr>
<td class="name"><code>messaging</code></td>
<td class="type">
<span class="param-type">Messaging</span>
</td>
<td class="description last"><p>A <code>Messaging</code> instance as defined in <a href="mailto:`@magic.batua">`@magic.batua</a>/messaging` package.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line35">line 35</a>
</li></ul></dd>
</dl>
</div>
<h3 class="subsection-title">Methods</h3>
<h4 class="name" id="Create"><span class="type-signature"></span>Create<span class="signature">(input)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Registers a new Magic Batua account and returns a stringified version of the
new <code>Account</code> object. The registration process is as follows:</p>
<ol>
<li>Initialise a new <code>Account</code> object using the given <code>input</code>.</li>
<li>Check for duplicate account</li>
<li>Send a verification SMS</li>
<li>If an <code>inviteCode</code> is provided in the <code>input</code> query, find the referrer.<ul>
<li>Add a new referral to the <code>referrer</code> account and award them Magic Points for a referral.</li>
</ul>
</li>
<li>Issue Magic Points to the new account.</li>
<li>Write the account to the database.</li>
</ol>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>input</code></td>
<td class="type">
<span class="param-type">SignupQuery</span>
</td>
<td class="description last"><p>See <code>index.ts</code> for definition of <code>SignupQuery</code>.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line65">line 65</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>A stringified version of the <code>Account</code> object</p>
</div>
<h5>Example</h5>
<pre class="prettyprint"><code>let registry = new Registry(...)
registry.Create({
name: "Godzilla"
phone: 1234567890,
email: "god@zilla.com",
password: "Password",
inviteCode: "BigInJapan" // Optional
})</code></pre>
<h4 class="name" id="DidResetPassword"><span class="type-signature"></span>DidResetPassword<span class="signature">(phone, newPass, pin)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Before a user can submit a reset password request, they need to verify their
identity via a one-time pin sent to their registered mobile number. This method
expects that <code>pin</code> as well as the <code>newPassword</code> as the input.</p>
<p> If OTP-verification succeeds, the <code>newPassword</code> is salted using a new randomly
generated salt and then hashed before being stored in the database. So in
effect, this method resets both the <code>salt</code> and the <code>password</code>.</p>
<p> If OTP-verification fails, password is not reset and an error is thrown instead.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>phone</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="description last"><p>Registered mobile number</p></td>
</tr>
<tr>
<td class="name"><code>newPass</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="description last"><p>New password</p></td>
</tr>
<tr>
<td class="name"><code>pin</code></td>
<td class="type">
<span class="param-type">pin</span>
</td>
<td class="description last"><p>OTP sent during verification</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line303">line 303</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p><code>true</code> if password reset is successful, otherwise throws an error.</p>
</div>
<h4 class="name" id="DidSendOTP"><span class="type-signature"></span>DidSendOTP<span class="signature">(phone)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Generates a random one-time verification pin and sends it to the given <code>phone</code>
number. The method is designed such that if the <code>phone</code> number is not registered
with us, the method will throw an error and refuse to send the SMS.</p>
<p> This could be problematic in some cases, so if a solid reason can be found to remove
this caveat, you should edit out the part of code in the beginning of the method.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>phone</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="description last"><p>A mobile number registered with us.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line243">line 243</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p><code>true</code> if the SMS was sent successfully, otherwise throws an error.</p>
</div>
<h4 class="name" id="HasVerified"><span class="type-signature"></span>HasVerified<span class="signature">(phone, pin)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Marks an account as verified if the given <code>pin</code> matches the one sent to the
account's registered mobile number.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>phone</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="description last"><p>Registered mobile number</p></td>
</tr>
<tr>
<td class="name"><code>pin</code></td>
<td class="type">
<span class="param-type">number</span>
</td>
<td class="description last"><p>OTP sent for verification</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line275">line 275</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p><code>true</code> if verification is successful, otherwise throws an error.</p>
</div>
<h4 class="name" id="IsDuplicate"><span class="type-signature"></span>IsDuplicate<span class="signature">(input)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Checks whether the given account parameters already exists in our records. Used
to prevent duplicate registrations.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>input</code></td>
<td class="type">
<span class="param-type">SignupQuery</span>
</td>
<td class="description last"><p>See <code>index.ts</code> for definition of <code>SignupQuery</code>.</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line50">line 50</a>
</li></ul></dd>
</dl>
<h4 class="name" id="Modify"><span class="type-signature"></span>Modify<span class="signature">(id, query)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Modifies profile information for the given account <code>_id</code> as instructed by the <code>query</code>
parameter. At the time of writing, only email, phone and name could be updated. For
changing/resetting password, use <a href="Registry.html#DidResetPassword"><code>DidResetPassword()</code></a>
instead.</p>
<p> <strong>This method doesn't perform validation on input data. So you could very well set the
phone as "0000" and it wouldn't bat an eye. This should be improved in the next version.</strong>.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>id</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="description last"><p>Magic Batua user <code>_id</code></p></td>
</tr>
<tr>
<td class="name"><code>query</code></td>
<td class="type">
<span class="param-type">any</span>
</td>
<td class="description last"><p>Key-value pairs to be updated</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line171">line 171</a>
</li></ul></dd>
</dl>
<h5>Example</h5>
<pre class="prettyprint"><code>let registry = new Registry(...)
registry.Modify("abcdefgh", {
phone: "1234567890",
name: "Godzilla"
})</code></pre>
<h4 class="name" id="Remove"><span class="type-signature"></span>Remove<span class="signature">(id)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Puts the account with ID <code>_id</code> under a 14-day deletion hold. If the account owner
doesn't logs into their account within this 14-day period, the account is permanently
<em>soft-deleted</em> and can't be recovered.</p>
<p> If a user does log in within the 14-day window, the deletion hold is lifted and the
account is restored back to normal. See <a href="Registry.html#Retrieve"><code>Retrieve()</code></a> for
the logic that removes the deletion hold.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>id</code></td>
<td class="type">
<span class="param-type">string</span>
</td>
<td class="description last"><p><code>_id</code> of the user to be deleted</p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line210">line 210</a>
</li></ul></dd>
</dl>
<h4 class="name" id="Retrieve"><span class="type-signature"></span>Retrieve<span class="signature">(query)</span><span class="type-signature"></span></h4>
<div class="description">
<p>Returns a stringified version of the <code>Account</code> object that matches the given <code>query</code>.
If the account requested had been marked for deletion earlier, and account <code>recoverBy</code>
date is in the future, the deletion hold on the account is lifted and the account is
restored to its former glory.</p>
<p> There is no separate function to lift the deletion hold on an account. After requesting
a deletion, a user has 14 days to cancel it by logging back into their account. After
the 14th day, the account is soft-deleted and can't be recovered.</p>
</div>
<h5>Parameters:</h5>
<table class="params">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th class="last">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="name"><code>query</code></td>
<td class="type">
<span class="param-type">LoginQuery</span>
</td>
<td class="description last"><p>See <code>index.ts</code> for definition of <code>LoginQuery</code></p></td>
</tr>
</tbody>
</table>
<dl class="details">
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="index.js.html">index.js</a>, <a href="index.js.html#line125">line 125</a>
</li></ul></dd>
</dl>
<h5>Returns:</h5>
<div class="param-desc">
<p>A stringified <code>Account</code> object</p>
</div>
<h5>Example</h5>
<pre class="prettyprint"><code>let registry = new Registry(...)
registry.Retrieve({
phone: "1234567890",
password: "Godzilla"
})</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated at Mon Mar 19 2018 22:39:23 GMT+0530 (IST)
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>