Merge pull request 'req: pengumuman' (#2) from amalia/14-jan-26 into join
Reviewed-on: http://wibugit.wibudev.com/wibu/sistem-desa-mandiri/pulls/2
This commit is contained in:
@@ -168,16 +168,29 @@ model Announcement {
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
AnnouncementMember AnnouncementMember[]
|
||||
AnnouncementFile AnnouncementFile[]
|
||||
}
|
||||
|
||||
model AnnouncementMember {
|
||||
id String @id @default(cuid())
|
||||
Announcement Announcement @relation(fields: [idAnnouncement], references: [id])
|
||||
idAnnouncement String
|
||||
Group Group @relation(fields: [idGroup], references: [id])
|
||||
idGroup String
|
||||
Division Division @relation(fields: [idDivision], references: [id])
|
||||
idDivision String
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
}
|
||||
|
||||
model AnnouncementFile {
|
||||
id String @id @default(cuid())
|
||||
Announcement Announcement @relation(fields: [idAnnouncement], references: [id])
|
||||
idAnnouncement String
|
||||
Group Group @relation(fields: [idGroup], references: [id])
|
||||
idGroup String
|
||||
Division Division @relation(fields: [idDivision], references: [id])
|
||||
idDivision String
|
||||
name String
|
||||
extension String
|
||||
idStorage String?
|
||||
isActive Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@/module/_global";
|
||||
import { DIR, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { createLogUserMobile } from "@/module/user";
|
||||
import _ from "lodash";
|
||||
@@ -76,13 +76,26 @@ export async function GET(request: Request, context: { params: { id: string } })
|
||||
// const fixMember = Object.groupBy(formatMember, ({ group }) => group);
|
||||
const fixMember = _.groupBy(formatMember, ({ group }) => group);
|
||||
|
||||
const file = await prisma.announcementFile.findMany({
|
||||
where: {
|
||||
idAnnouncement: id
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
idStorage: true,
|
||||
name: true,
|
||||
extension: true
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan pengumuman",
|
||||
data: announcement,
|
||||
member: fixMember
|
||||
member: fixMember,
|
||||
file: file
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
@@ -154,7 +167,12 @@ export async function DELETE(request: Request, context: { params: { id: string }
|
||||
// EDIT PENGUMUMAN
|
||||
export async function PUT(request: Request, context: { params: { id: string } }) {
|
||||
try {
|
||||
const { title, desc, groups, user } = (await request.json());
|
||||
const body = await request.formData()
|
||||
const dataBody = body.get("data")
|
||||
const cekFile = body.has("file0")
|
||||
|
||||
// const { title, desc, groups, user } = (await request.json());
|
||||
const { title, desc, groups, user, oldFile } = JSON.parse(dataBody as string)
|
||||
const { id } = context.params;
|
||||
const userMobile = await funGetUserById({ id: String(user) })
|
||||
|
||||
@@ -214,6 +232,41 @@ export async function PUT(request: Request, context: { params: { id: string } })
|
||||
data: memberDivision,
|
||||
});
|
||||
|
||||
if (oldFile.length > 0) {
|
||||
for (let index = 0; index < oldFile.length; index++) {
|
||||
const element = oldFile[index];
|
||||
if (element.delete) {
|
||||
await prisma.announcementFile.delete({
|
||||
where: {
|
||||
id: element.id
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cekFile) {
|
||||
body.delete("data")
|
||||
for (var pair of body.entries()) {
|
||||
if (String(pair[0]).substring(0, 4) == "file") {
|
||||
const file = body.get(pair[0]) as File
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = decodeURIComponent(file.name.replace("." + fExt, ""))
|
||||
const upload = await funUploadFile({ file: file, dirId: DIR.announcement })
|
||||
if (upload.success) {
|
||||
await prisma.announcementFile.create({
|
||||
data: {
|
||||
idStorage: upload.data.id,
|
||||
idAnnouncement: id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create log user
|
||||
const log = await createLogUserMobile({ act: 'UPDATE', desc: 'User mengupdate data pengumuman', table: 'announcement', data: id, user: userMobile.id })
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { funSendWebPush, prisma } from "@/module/_global";
|
||||
import { DIR, funSendWebPush, funUploadFile, prisma } from "@/module/_global";
|
||||
import { funGetUserById } from "@/module/auth";
|
||||
import { createLogUserMobile } from '@/module/user';
|
||||
import _ from "lodash";
|
||||
@@ -113,7 +113,12 @@ export async function GET(request: Request) {
|
||||
// CREATE PENGUMUMAN
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const { title, desc, groups, user } = (await request.json());
|
||||
const body = await request.formData()
|
||||
const dataBody = body.get("data")
|
||||
const cekFile = body.has("file0")
|
||||
|
||||
// const { title, desc, groups, user } = (await request.json());
|
||||
const { title, desc, groups, user } = JSON.parse(dataBody as string)
|
||||
const userMobile = await funGetUserById({ id: String(user) })
|
||||
|
||||
if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
|
||||
@@ -152,6 +157,29 @@ export async function POST(request: Request) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (cekFile) {
|
||||
body.delete("data")
|
||||
for (var pair of body.entries()) {
|
||||
if (String(pair[0]).substring(0, 4) == "file") {
|
||||
const file = body.get(pair[0]) as File
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = decodeURIComponent(file.name.replace("." + fExt, ""))
|
||||
const upload = await funUploadFile({ file: file, dirId: DIR.announcement })
|
||||
if (upload.success) {
|
||||
await prisma.announcementFile.create({
|
||||
data: {
|
||||
idStorage: upload.data.id,
|
||||
idAnnouncement: data.id,
|
||||
name: fName,
|
||||
extension: String(fExt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const announcementMember = await prisma.announcementMember.createMany({
|
||||
data: memberDivision,
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { hookstate } from "@hookstate/core"
|
||||
import { IGlobalTema } from './type_global';
|
||||
import { hookstate } from "@hookstate/core";
|
||||
import { RefObject } from "react";
|
||||
import { IGlobalTema } from './type_global';
|
||||
|
||||
export const pwd_key_config = "fchgvjknlmdfnbvghhujlaknsdvjbhknlkmsdbdyu567t8y9u30r4587638y9uipkoeghjvuyi89ipkoefmnrjbhtiu4or9ipkoemnjfbhjiuoijdklnjhbviufojkejnshbiuojijknehgruyu"
|
||||
export const globalRole = hookstate<string>('')
|
||||
@@ -11,7 +11,8 @@ export const DIR = {
|
||||
document: "cm0xhbkf50009acbbtw03qo4l",
|
||||
village: "cm0xhb91o0007acbbkx8rk8hj",
|
||||
user: "cm0x8dbwn0005bp5tgmfcthzw",
|
||||
banner: "cm1sxex19004938bjvyaq8vta"
|
||||
banner: "cm1sxex19004938bjvyaq8vta",
|
||||
announcement: "cmkdfkze4005hkhjgunsroi4t"
|
||||
}
|
||||
|
||||
export const keyWibu = 'padahariminggukuturutayahkekotanaikdelmanistimewakududukdimuka'
|
||||
|
||||
Reference in New Issue
Block a user