Skip to content

Commit

Permalink
add: data field to tasks and handle saving images (#3)
Browse files Browse the repository at this point in the history
* add: data field to tasks and handle saving images

* handle directory update

* add: directory update route
  • Loading branch information
Himali-Malvawala authored Jun 18, 2024
1 parent 771510d commit cda5196
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/controllers/TaskController.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<interfaces.IHttpActionResult> {
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<interfaces.IHttpActionResult> {
return this.actionWrapper(req, res, async (au) => {
Expand Down Expand Up @@ -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;
Expand All @@ -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";
}
}

}
1 change: 1 addition & 0 deletions src/models/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export class Task {
public status?: string;
public automationId?: string;
public conversationId?: string;
public data?: string;
}
12 changes: 8 additions & 4 deletions src/repositories/TaskRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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]);
}
Expand Down
1 change: 1 addition & 0 deletions tools/dbScripts/tasks.mysql
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
);

0 comments on commit cda5196

Please sign in to comment.