Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record invite confirmed state #49

Merged
merged 2 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, maybe we should just make getting the membersCollection and operationsCollection a function that takes in db, since these are sorta redundant

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, created #51 to track.

},
{
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