Skip to content

Commit

Permalink
Record invite confirmed state (#49)
Browse files Browse the repository at this point in the history
Add the invite_confirmed field to members and update it to true when
a member is trusted by the member they requested an invite from.
  • Loading branch information
rahulgi authored Jun 22, 2018
1 parent c1be2f3 commit f6380cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const apiRoutes: Array<RouteHandler<ApiLocation>> = [
},
{
location: trustMemberApiLocation,
handler: membersRoutes.trust(membersCollection, operationsCollection)
handler: membersRoutes.trust(db, membersCollection, operationsCollection)
},
{
location: requestInviteApiLocation,
Expand Down
62 changes: 46 additions & 16 deletions src/routes/members/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export const requestInvite = (
username,
full_name: fullName,
request_invite_from_uid: requestingFromId,
invite_confirmed: false,
created_at: firestore.FieldValue.serverTimestamp(),
request_invite_block_at: null,
request_invite_block_seq: null,
Expand Down Expand Up @@ -286,29 +287,58 @@ export const requestInvite = (
* Create a trust relationship to a target member from the logged in member
*/
export const trust = (
db: Firestore,
membersCollection: CollectionReference,
operationsCollection: CollectionReference
) =>
createApiRoute<TrustMemberApiEndpoint>(async (call, loggedInMemberToken) => {
const loggedInUid = loggedInMemberToken.uid;
const memberToTrustId = call.params.memberId;
if (!(await getMemberById(membersCollection, memberToTrustId))) {
throw new ApiError(httpStatus.NOT_FOUND, "Member to trust not found.");
}
const newOperationReference = await db.runTransaction(async transaction => {
const loggedInUid = loggedInMemberToken.uid;
const memberToTrustId = call.params.memberId;
const memberToTrust = await transaction.get(
membersCollection.doc(memberToTrustId)
);

if (!memberToTrust) {
throw new ApiError(httpStatus.NOT_FOUND, "Member to trust not found.");
}
if (
!(await transaction.get(
operationsCollection
.where("creator_uid", "==", loggedInUid)
.where("op_code", "==", OperationType.TRUST)
.where("data.to_uid", "==", memberToTrustId)
)).empty
) {
throw new ApiError(
httpStatus.BAD_REQUEST,
"You have already trusted this member."
);
}

const newOperation: OperationToBeCreated = {
creator_uid: loggedInUid,
op_code: OperationType.TRUST,
data: {
to_uid: memberToTrustId
},
created_at: firestore.FieldValue.serverTimestamp()
};
const newOperationRef = operationsCollection.doc();
if (memberToTrust.get("request_invite_from_uid") === loggedInUid) {
transaction.update(memberToTrust.ref, {
invite_confirmed: true
});
}
transaction.set(newOperationRef, newOperation);

return newOperationRef;
});

const newOperation: OperationToBeCreated = {
creator_uid: loggedInUid,
op_code: OperationType.TRUST,
data: {
to_uid: memberToTrustId
},
created_at: firestore.FieldValue.serverTimestamp()
};
const newOperationDoc = await operationsCollection.add(newOperation);
return {
body: {
...(await newOperationDoc.get()).data(),
id: newOperationDoc.id
...(await newOperationReference.get()).data(),
id: newOperationReference.id
} as Operation,
status: 201
};
Expand Down

0 comments on commit f6380cf

Please sign in to comment.