diff --git a/src/controllers/TaskController.ts b/src/controllers/TaskController.ts index cfa5bef..c5c8dc1 100644 --- a/src/controllers/TaskController.ts +++ b/src/controllers/TaskController.ts @@ -1,7 +1,9 @@ import { controller, httpPost, httpGet, interfaces, requestParam } from "inversify-express-utils"; import express from "express"; +import { FileStorageHelper } from "@churchapps/apihelper"; import { DoingBaseController } from "./DoingBaseController" import { Task } from "../models" +import { Environment } from "../helpers"; @controller("/tasks") export class TaskController extends DoingBaseController { @@ -21,6 +23,13 @@ export class TaskController extends DoingBaseController { }); } + @httpGet("/directoryUpdate/:personId") + public async getPersonDirectoryUpdate(@requestParam("personId") personId: string, req: express.Request<{}, {}, null>, res: express.Response): Promise { + return this.actionWrapper(req, res, async (au) => { + return await this.repositories.task.loadForDirectoryUpdate(au.churchId, personId); + }); + } + @httpGet("/:id") public async get(@requestParam("id") id: string, req: express.Request<{}, {}, null>, res: express.Response): Promise { return this.actionWrapper(req, res, async (au) => { @@ -48,6 +57,7 @@ export class TaskController extends DoingBaseController { const result: Task[] = [] for (const task of req.body) { task.churchId = au.churchId; + if (req.query?.type === "directoryUpdate") await this.handleDirectoryUpdate(au.churchId, task); result.push(await this.repositories.task.save(task)); } return result; @@ -63,4 +73,26 @@ export class TaskController extends DoingBaseController { } */ + private async savePhoto(churchId: string, base64Str: string, task: Task) { + const base64 = base64Str.split(",")[1]; + const key = "/" + churchId + "/membership/people/" + task.associatedWithId + ".png"; + await FileStorageHelper.store(key, "image/png", Buffer.from(base64, "base64")); + const photoUpdated = new Date(); + const photo: string = Environment.contentRoot + key + "?dt=" + photoUpdated.getTime().toString(); + return photo; + } + + private async handleDirectoryUpdate (churchId: string, task: Task) { + if (task.status === "Open") { + const data = JSON.parse(task.data); + for (const d of data) { + if (d.field === "photo" && d.value !== undefined) { + d.value = await this.savePhoto(churchId, d.value, task); + } + } + task.data = JSON.stringify(data); + task.taskType = "directoryUpdate"; + } + } + } \ No newline at end of file diff --git a/src/models/Task.ts b/src/models/Task.ts index 09f5839..42804a9 100644 --- a/src/models/Task.ts +++ b/src/models/Task.ts @@ -18,4 +18,5 @@ export class Task { public status?: string; public automationId?: string; public conversationId?: string; + public data?: string; } \ No newline at end of file diff --git a/src/repositories/TaskRepository.ts b/src/repositories/TaskRepository.ts index 0d419f3..77c3703 100644 --- a/src/repositories/TaskRepository.ts +++ b/src/repositories/TaskRepository.ts @@ -34,15 +34,15 @@ export class TaskRepository { task.id = UniqueIdHelper.shortId(); const taskNumber = await this.loadNextTaskNumber(task.churchId); // NOTE - This is problematic if saving multiple records asyncronously. Be sure to await each call - const sql = "INSERT INTO tasks (id, churchId, taskNumber, taskType, dateCreated, dateClosed, associatedWithType, associatedWithId, associatedWithLabel, createdByType, createdById, createdByLabel, assignedToType, assignedToId, assignedToLabel, title, status, automationId, conversationId) VALUES (?, ?, ?, ?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; - const params = [task.id, task.churchId, taskNumber, task.taskType, task.dateClosed, task.associatedWithType, task.associatedWithId, task.associatedWithLabel, task.createdByType, task.createdById, task.createdByLabel, task.assignedToType, task.assignedToId, task.assignedToLabel, task.title, task.status, task.automationId, task.conversationId]; + const sql = "INSERT INTO tasks (id, churchId, taskNumber, taskType, dateCreated, dateClosed, associatedWithType, associatedWithId, associatedWithLabel, createdByType, createdById, createdByLabel, assignedToType, assignedToId, assignedToLabel, title, status, automationId, conversationId, data) VALUES (?, ?, ?, ?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; + const params = [task.id, task.churchId, taskNumber, task.taskType, task.dateClosed, task.associatedWithType, task.associatedWithId, task.associatedWithLabel, task.createdByType, task.createdById, task.createdByLabel, task.assignedToType, task.assignedToId, task.assignedToLabel, task.title, task.status, task.automationId, task.conversationId, task.data]; await DB.query(sql, params); return task; } private async update(task: Task) { - const sql = "UPDATE tasks SET taskType=?, dateCreated=?, dateClosed=?, associatedWithType=?, associatedWithId=?, associatedWithLabel=?, createdByType=?, createdById=?, createdByLabel=?, assignedToType=?, assignedToId=?, assignedToLabel=?, title=?, status=?, automationId=?, conversationId=? WHERE id=? and churchId=?"; - const params = [task.taskType, task.dateCreated, task.dateClosed, task.associatedWithType, task.associatedWithId, task.associatedWithLabel, task.createdByType, task.createdById, task.createdByLabel, task.assignedToType, task.assignedToId, task.assignedToLabel, task.title, task.status, task.automationId, task.conversationId, task.id, task.churchId]; + const sql = "UPDATE tasks SET taskType=?, dateCreated=?, dateClosed=?, associatedWithType=?, associatedWithId=?, associatedWithLabel=?, createdByType=?, createdById=?, createdByLabel=?, assignedToType=?, assignedToId=?, assignedToLabel=?, title=?, status=?, automationId=?, conversationId=?, data=? WHERE id=? and churchId=?"; + const params = [task.taskType, task.dateCreated, task.dateClosed, task.associatedWithType, task.associatedWithId, task.associatedWithLabel, task.createdByType, task.createdById, task.createdByLabel, task.assignedToType, task.assignedToId, task.assignedToLabel, task.title, task.status, task.automationId, task.conversationId, task.data, task.id, task.churchId]; await DB.query(sql, params); return task; } @@ -104,6 +104,10 @@ export class TaskRepository { else return DB.query("SELECT * FROM tasks WHERE churchId=? AND ((assignedToType='group' AND assignedToId IN (?)) OR (createdByType='group' AND createdById IN (?))) AND status=? order by taskNumber;", [churchId, groupIds, groupIds, status]); } + public async loadForDirectoryUpdate(churchId: string, personId: string) { + return DB.query("SELECT * FROM tasks WHERE taskType='directoryUpdate' AND status='Open' AND churchId=? AND associatedWithId=?;", [churchId, personId]); + } + public loadAll(churchId: string) { return DB.query("SELECT * FROM tasks WHERE churchId=?;", [churchId]); } diff --git a/tools/dbScripts/tasks.mysql b/tools/dbScripts/tasks.mysql index 15bc868..ddffa63 100644 --- a/tools/dbScripts/tasks.mysql +++ b/tools/dbScripts/tasks.mysql @@ -20,5 +20,6 @@ CREATE TABLE `tasks` ( `status` varchar(45) DEFAULT NULL, `automationId` char(11) DEFAULT NULL, `conversationId` char(11) DEFAULT NULL, + `data` text, PRIMARY KEY (`id`) );