generated from HelloUnspecified/template-graph-server
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from ThatConference/feat/my-network
Feat/my network
- Loading branch information
Showing
30 changed files
with
2,276 additions
and
1,149 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import debug from 'debug'; | ||
import { utility } from '@thatconference/api'; | ||
|
||
const dlog = debug('that:api:members:datasources:sharing-with'); | ||
const collectionName = 'members'; | ||
const subCollectionName = 'sharingWith'; | ||
const collectionGroupName = 'sharingWith'; | ||
const { entityDateForge } = utility.firestoreDateForge; | ||
const fields = ['createdAt', 'lastUpdatedAt']; | ||
const sharingDataDateForge = entityDateForge({ fields }); | ||
|
||
const scrubSharingData = (sharingData, isNew = false) => { | ||
dlog('scrubing sharing data'); | ||
const scrubbedSharingData = sharingData; | ||
const now = new Date(); | ||
if (isNew) { | ||
scrubbedSharingData.createdAt = now; | ||
} | ||
scrubbedSharingData.lastUpdatedAt = now; | ||
return scrubbedSharingData; | ||
}; | ||
|
||
const sharingWith = dbInstance => { | ||
dlog('sharingWith db instance created'); | ||
|
||
const memberCollection = dbInstance.collection(collectionName); | ||
|
||
function get({ sharedById, sharedWithId }) { | ||
dlog('get sharing record by %s for %s', sharedById, sharedWithId); | ||
return memberCollection | ||
.doc(sharedById) | ||
.collection(subCollectionName) | ||
.doc(sharedWithId) | ||
.get() | ||
.then(docSnap => { | ||
let doc = null; | ||
if (docSnap.exists) { | ||
doc = { | ||
id: docSnap.id, | ||
...docSnap.data(), | ||
}; | ||
} | ||
return sharingDataDateForge(doc); | ||
}); | ||
} | ||
|
||
function findSharedById(memberId) { | ||
dlog('findShaedById for %s', memberId); | ||
return memberCollection | ||
.doc(memberId) | ||
.collection(subCollectionName) | ||
.get() | ||
.then(querySnap => | ||
querySnap.docs.map(docs => { | ||
const doc = { | ||
id: docs.id, | ||
...docs.data(), | ||
}; | ||
return sharingDataDateForge(doc); | ||
}), | ||
); | ||
} | ||
|
||
function findSharedWithId(memberId) { | ||
dlog('findsharedwithId for %s', memberId); | ||
return dbInstance | ||
.collectionGroup(collectionGroupName) | ||
.where('sharedWithId', '==', memberId) | ||
.get() | ||
.then(querySnap => | ||
querySnap.docs.map(docs => { | ||
const doc = { | ||
id: docs.id, | ||
...docs.data(), | ||
}; | ||
return sharingDataDateForge(doc); | ||
}), | ||
); | ||
} | ||
|
||
function add({ sharedById, sharedWithId, sharingData = {} }) { | ||
dlog('add new member sharing for %s with %s', sharedById, sharedWithId); | ||
if (!sharedById || !sharedWithId) | ||
throw new Error( | ||
'SharedById and SharedWidthId are both required to add a sharing', | ||
); | ||
const newSharingData = { | ||
...sharingData, | ||
sharedById, | ||
sharedWithId, | ||
}; | ||
scrubSharingData(newSharingData, true); | ||
|
||
return memberCollection | ||
.doc(sharedById) | ||
.collection(subCollectionName) | ||
.doc(sharedWithId) | ||
.create(newSharingData) | ||
.then(() => get({ sharedById, sharedWithId })); | ||
} | ||
|
||
function update({ sharedById, sharedWithId, sharingData = {} }) { | ||
dlog( | ||
'update member sharing for %s with %s: %o', | ||
sharedById, | ||
sharedWithId, | ||
sharingData, | ||
); | ||
if (!sharedById || !sharedWithId) | ||
throw new Error( | ||
'SharedById and SharedWidthId are both required to update a sharing', | ||
); | ||
// Ensure share by and shared with don't change, they can't be updated | ||
const updateSharingData = { | ||
...sharingData, | ||
sharedById, | ||
sharedWithId, | ||
}; | ||
scrubSharingData(updateSharingData, false); | ||
|
||
return memberCollection | ||
.doc(sharedById) | ||
.collection(subCollectionName) | ||
.doc(sharedWithId) | ||
.update(updateSharingData) | ||
.then(() => get({ sharedById, sharedWithId })); | ||
} | ||
|
||
function remove({ sharedById, sharedWithId }) { | ||
dlog('remove sharing by %s for %s', sharedById, sharedWithId); | ||
if (!sharedById || !sharedWithId) | ||
throw new Error( | ||
'sharedById and sharedWidthId are both required to remove a sharing', | ||
); | ||
return memberCollection | ||
.doc(sharedById) | ||
.collection(subCollectionName) | ||
.doc(sharedWithId) | ||
.delete() | ||
.then(() => ({ sharedById, sharedWithId })); | ||
} | ||
|
||
return { get, findSharedById, findSharedWithId, add, update, remove }; | ||
}; | ||
|
||
export default sharingWith; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,35 @@ function userEvents(postmark) { | |
); | ||
} | ||
|
||
function sendNewShareEmail({ | ||
sharingWith, | ||
sharingSharedProfile, | ||
messageToShareWith = null, | ||
}) { | ||
dlog('sendNewShareEmail'); | ||
return postmark | ||
.sendEmailWithTemplate({ | ||
TemplateAlias: 'network-new-share-with-you', | ||
From: '[email protected]', | ||
To: sharingWith.email, | ||
Tag: 'network-sharing', | ||
TemplateModel: { | ||
sharingWith: { | ||
firstName: sharingWith.firstName, | ||
lastName: sharingWith.lastName, | ||
email: sharingWith.email, | ||
}, | ||
sharingSharedProfile, | ||
messageToShareWith, | ||
}, | ||
}) | ||
.catch(err => | ||
process.nextTick(() => | ||
userEventEmitter.emit('error', { err, user: sharingSharedProfile }), | ||
), | ||
); | ||
} | ||
|
||
userEventEmitter.on('error', ({ err, user }) => { | ||
Sentry.setTag('section', 'userEventEmitter'); | ||
Sentry.setContext('user object', { user }); | ||
|
@@ -134,6 +163,8 @@ function userEvents(postmark) { | |
userEventEmitter.on('accountCreated', sendOrbitLoveActivityOnCreate); | ||
userEventEmitter.on('accountUpdated', sendOrbitLoveActivityOnUpdate); | ||
|
||
userEventEmitter.on('addNewSharingWith', sendNewShareEmail); | ||
|
||
return userEventEmitter; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import debug from 'debug'; | ||
|
||
const dlog = debug('that:api:members:mutation:members-network'); | ||
|
||
export const fieldResolvers = { | ||
MembersNetworkMutation: { | ||
add: () => ({}), | ||
|
||
sharingWith: (_, { sharedWithId }) => { | ||
dlog('enter sharingWith path for %s', sharedWithId); | ||
|
||
return { sharedWithId }; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import debug from 'debug'; | ||
import sharingWithStore from '../../../dataSources/cloudFirestore/sharingWith'; | ||
|
||
const dlog = debug('that:api:members:network-sharing-with-mutation'); | ||
|
||
export const fieldResolvers = { | ||
NetworkSharingWithMutation: { | ||
update: ( | ||
{ sharedWithId }, | ||
{ shareWith }, | ||
{ dataSources: { firestore }, user }, | ||
) => { | ||
dlog('update shared with %s', sharedWithId); | ||
|
||
return sharingWithStore(firestore).update({ | ||
sharedById: user.sub, | ||
sharedWithId, | ||
sharingData: { | ||
notes: shareWith?.notes ?? '', | ||
}, | ||
}); | ||
}, | ||
appendNewNote: (_, { text }) => { | ||
dlog('appending text length %d to notes', text?.length); | ||
|
||
throw new Error('Not Implemented'); | ||
}, | ||
remove: ({ sharedWithId }, __, { dataSources: { firestore }, user }) => { | ||
dlog('removing share with %s', sharedWithId); | ||
|
||
// return sharedWithId; | ||
return sharingWithStore(firestore) | ||
.remove({ sharedById: user.sub, sharedWithId }) | ||
.then(result => result.sharedWithId); | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.