Created
July 16, 2020 07:26
-
-
Save ranyefet/01ec5d43fc1487aac9bf8aee0994411d to your computer and use it in GitHub Desktop.
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
const encodeBase64 = (str) => Buffer.from(str).toString('base64'); | |
interface Event { | |
id: String; | |
title: String; | |
type: String; | |
severity: String; | |
category: String; | |
createdAt: Date; | |
modifiedAt: Date; | |
} | |
function eventTransformer(eventData) : Event { | |
const { _id, _key, ...event } = eventData; | |
return { | |
id: encodeBase64(_id), | |
...event, | |
}; | |
} | |
class EventService { | |
constructor(private viewer, private repo) {} | |
async list() { | |
// check authorization with this.viewer | |
const events = await this.repo.find(); | |
return events.map(eventTransformer); | |
} | |
async byId(id: String) { | |
const event = await this.repo.loadById(id); | |
if (!event) { | |
return null; | |
} | |
// check authorization with this.viewer | |
if (event.createdBy !== this.viewer.id) { | |
return null; | |
} | |
return eventTransformer(event); | |
} | |
} | |
class EventRepository { | |
private eventLoader; | |
constructor(private db) { | |
this.eventLoader = new DataLoader(async (ids) => { | |
return await db.query(aql` | |
FOR event IN events | |
FILTER event.id IN ${ids} | |
RETURN event | |
`); | |
}); | |
} | |
async find({ filter, skip, limit, order }) { | |
// | |
return await db.query(aql` | |
FOR event IN events | |
RETURN event; | |
`); | |
} | |
async loadById(id) { | |
return await this.eventLoader.load(id); | |
} | |
async loadEventsByFilter({ relation = 'identity', id = '1' }) { | |
if (relation === 'identity') { | |
return await this.identityEventsLoader.load(id); | |
} else { | |
return await this.resourceEventsLoader.load(id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment