Skip to content
This repository has been archived by the owner on Jul 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #43 from BenShelton/develop
Browse files Browse the repository at this point in the history
Reorganise data into separate languages
  • Loading branch information
BenShelton authored Feb 13, 2019
2 parents 0672c13 + 9f644a8 commit 800a86e
Show file tree
Hide file tree
Showing 18 changed files with 954 additions and 369 deletions.
84 changes: 63 additions & 21 deletions database/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'assert'
import setup from './setup'
import scrapeWOL from './scraper'
import { addAssignment, removeAssignment } from './congregation'
import { WEEK_TYPES } from '../src/constants'
import { WEEK_TYPES, SUPPORTED_LANGUAGES } from '../src/constants'

const ASSIGNEE_FIELDS = ['assignee', 'assistant']

Expand All @@ -19,7 +19,11 @@ export const getWeek = async ({ date }) => {
const existingWeek = await coll.findOne(query)
if (existingWeek) return existingWeek
// if week doesn't exist add it and return the added week
const newWeekResult = await coll.insertOne({ date, assignments: {} })
const baseWeek = { date }
for (const { value } of SUPPORTED_LANGUAGES) {
baseWeek[value] = { assignments: {} }
}
const newWeekResult = await coll.insertOne(baseWeek)
const newWeek = newWeekResult && newWeekResult.ops && newWeekResult.ops[0]
assert.notStrictEqual(null, newWeek, 'Adding Missing Week was unsuccessful')
return newWeek
Expand All @@ -45,46 +49,48 @@ export const getMonth = async ({ month }) => {
return weeks
}

export const scrapeWeek = async ({ weekID }) => {
export const scrapeWeek = async ({ weekID, language }) => {
const coll = await getCollection
const query = { _id: ObjectID(weekID) }
const week = await coll.findOne(query)
assert.notStrictEqual(null, week, 'Week ID does not exist')
const update = { $set: await scrapeWOL(week.date) }
const update = { $set: await scrapeWOL(week.date, language) }
const { value } = await coll.findOneAndUpdate(query, update, { returnOriginal: false })
assert.notStrictEqual(null, value, 'Update not successful')
return value
}

export const updateAssignment = async ({ weekID, name, assignment }) => {
export const updateAssignment = async ({ weekID, language, name, assignment }) => {
const coll = await getCollection
const query = { _id: ObjectID(weekID) }

// Remove existing assigned members
const updatedMembers = []
const week = await coll.findOne(query)
const baseWeek = await coll.findOne(query)
assert.notStrictEqual(null, baseWeek, 404)
const week = baseWeek[language] || null
assert.notStrictEqual(null, week, 404)
const previousAssignment = week.assignments[name]
if (previousAssignment) {
for (const field of ASSIGNEE_FIELDS) {
const memberID = previousAssignment[field]
if (memberID) {
const member = await removeAssignment({ memberID, assignment: { type: previousAssignment.type, date: week.date } })
const member = await removeAssignment({ memberID, assignment: { type: previousAssignment.type, date: baseWeek.date } })
updatedMembers.push(member)
}
}
}

// Update Assignment
const update = {}
const assignmentPath = 'assignments.' + name
const assignmentPath = language + '.assignments.' + name
update.$set = { [assignmentPath]: assignment }
const { value } = await coll.findOneAndUpdate(query, update, { returnOriginal: false })
assert.notStrictEqual(null, value, 404)

// Add newly assigned members
for (const field of ASSIGNEE_FIELDS) {
const memberID = value.assignments[name][field]
const memberID = value[language].assignments[name][field]
if (memberID) {
const member = await addAssignment({ memberID, assignment: { type: assignment.type, date: value.date } })
updatedMembers.push(member)
Expand All @@ -95,13 +101,46 @@ export const updateAssignment = async ({ weekID, name, assignment }) => {
return { week: value, members: updatedMembers }
}

export const updateWeekType = async ({ weekID, type }) => {
export const deleteAssignment = async ({ weekID, language, name }) => {
const coll = await getCollection
const query = { _id: ObjectID(weekID) }

// Check the previous week didn't already have the same type
const week = await coll.findOne(query)
// Remove existing assigned members
const updatedMembers = []
const baseWeek = await coll.findOne(query)
assert.notStrictEqual(null, baseWeek, 404)
const week = baseWeek[language] || null
assert.notStrictEqual(null, week, 404)
const previousAssignment = week.assignments[name]
if (previousAssignment) {
for (const field of ASSIGNEE_FIELDS) {
const memberID = previousAssignment[field]
if (memberID) {
const member = await removeAssignment({ memberID, assignment: { type: previousAssignment.type, date: baseWeek.date } })
updatedMembers.push(member)
}
}
}

// Update Assignment
const update = {}
const assignmentPath = language + '.assignments.' + name
update.$unset = { [assignmentPath]: '' }
const { value } = await coll.findOneAndUpdate(query, update, { returnOriginal: false })
assert.notStrictEqual(null, value, 404)

// Return assignment
return { week: value, members: updatedMembers }
}

export const updateWeekType = async ({ weekID, language, type }) => {
const coll = await getCollection
const query = { _id: ObjectID(weekID) }

// Check the previous week didn't already have the same type
const baseWeek = await coll.findOne(query)
assert.notStrictEqual(null, baseWeek, 404)
const week = baseWeek[language] || {}
assert.notStrictEqual(type, week.type, 400)

// Remove existing assigned members if necessary
Expand All @@ -115,9 +154,9 @@ export const updateWeekType = async ({ weekID, type }) => {
for (const field of ASSIGNEE_FIELDS) {
const memberID = v[field]
if (memberID) {
const member = await removeAssignment({ memberID, assignment: { type: v.type, date: week.date } })
const member = await removeAssignment({ memberID, assignment: { type: v.type, date: baseWeek.date } })
updatedMembers.push(member)
const assignmentPath = `assignments.${k}.${field}`
const assignmentPath = `${language}.assignments.${k}.${field}`
update.$set[assignmentPath] = null
}
}
Expand All @@ -132,9 +171,9 @@ export const updateWeekType = async ({ weekID, type }) => {
for (const field of ASSIGNEE_FIELDS) {
const memberID = assignment[field]
if (memberID) {
const member = await removeAssignment({ memberID, assignment: { type: assignment.type, date: week.date } })
const member = await removeAssignment({ memberID, assignment: { type: assignment.type, date: baseWeek.date } })
updatedMembers.push(member)
const assignmentPath = `assignments.${assignmentName}.${field}`
const assignmentPath = `${language}.assignments.${assignmentName}.${field}`
update.$set[assignmentPath] = null
}
}
Expand All @@ -143,27 +182,30 @@ export const updateWeekType = async ({ weekID, type }) => {
}

// Update Type
Object.assign(update.$set, { type })
const updatePath = language + '.type'
Object.assign(update.$set, { [updatePath]: type })
const { value } = await coll.findOneAndUpdate(query, update, { returnOriginal: false })
assert.notStrictEqual(null, value, 404)

// Return week and any removed assigned members
return { week: value, members: updatedMembers }
}

export const updateCOName = async ({ weekID, name }) => {
export const updateCOName = async ({ weekID, language, name }) => {
const coll = await getCollection
const query = { _id: ObjectID(weekID) }
const update = { $set: { coName: name } }
const updatePath = language + '.coName'
const update = { $set: { [updatePath]: name } }
const { value } = await coll.findOneAndUpdate(query, update, { returnOriginal: false })
assert.notStrictEqual(null, value, 404)
return value
}

export const updateCOTitle = async ({ weekID, title }) => {
export const updateCOTitle = async ({ weekID, language, title }) => {
const coll = await getCollection
const query = { _id: ObjectID(weekID) }
const update = { $set: { coTitle: title } }
const updatePath = language + '.coTitle'
const update = { $set: { [updatePath]: title } }
const { value } = await coll.findOneAndUpdate(query, update, { returnOriginal: false })
assert.notStrictEqual(null, value, 404)
return value
Expand Down
Loading

0 comments on commit 800a86e

Please sign in to comment.