Untitled
unknown
typescript
3 years ago
8.2 kB
3
Indexable
import { Request, Response } from 'lambda-api'; import { ClientBase } from 'pg'; import { failure, success } from '../../../shared/libs/response'; import { getAuth } from '../../../shared/libs/auth'; import code from '../../../shared/libs/code'; import { AlbumModel } from '../../../shared/models/album.model'; import { s3GetPreSignedUrl } from '../../../shared/libs/s3'; import { PhotoModel } from '../../../shared/models/photos.model'; /** * * @param db * @param auth * @param body */ async function queryInsertGalleryAlbum( db: ClientBase, auth: any, body: AlbumModel ): Promise<AlbumModel> { try { const queryResult = await db.query( ` INSERT INTO gallery_album ( tenant_uuid, album_name, album_description, school_level_relation_id, school_location_id, school_level_id, created_by, created_at) VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING gallery_album_id, album_name, album_description, school_level_relation_id, school_location_id, school_level_id, created_by `, [ auth.tenant_uuid, body.album_name, body.album_description, body.school_level_relation_id, body.school_location_id, body.school_level_id, body.created_by, new Date(), ] ); if (queryResult.rowCount === 0) { return null; } return queryResult.rows[0]; } catch (e) { console.error('[ERROR-QUERY] - func: queryInsertGalleryAlbum', e); throw Error(code.database_error); } } async function queryInsertPublishPhotoDashboard( db: ClientBase, auth: any, albumId: number, body: AlbumModel ): Promise<AlbumModel> { try { const queryResult = await db.query( ` INSERT INTO gallery_publish_photo_on_dashboard ( tenant_uuid, gallery_album_id, publish_date, end_date, created_at) VALUES($1, $2, $3, $4, $5) RETURNING gallery_publish_photo_on_dashboard_id, gallery_album_id, publish_date, end_date, created_at `, [auth.tenant_uuid, albumId, body.dashboard_publish_date, body.dashboard_end_date, new Date()] ); if (queryResult.rowCount === 0) { return null; } return queryResult.rows[0]; } catch (e) { console.error('[ERROR-QUERY] - func: queryInsertPublishPhotoDashboard', e); throw Error(code.database_error); } } async function queryInsertPublishPhotoGallery( db: ClientBase, auth: any, albumId: number, body: AlbumModel ): Promise<AlbumModel> { try { const queryResult = await db.query( ` INSERT INTO gallery_publish_photo_on_gallery ( tenant_uuid, gallery_album_id, publish_date, end_date, created_at) VALUES($1, $2, $3, $4, $5) RETURNING gallery_publish_photo_on_gallery_id, gallery_album_id, publish_date, end_date, created_at `, [auth.tenant_uuid, albumId, body.gallery_publish_date, body.gallery_end_date, new Date()] ); if (queryResult.rowCount === 0) { return null; } return queryResult.rows[0]; } catch (e) { console.error('[ERROR-QUERY] - func: queryInsertPublishPhotoGallery', e); throw Error(code.database_error); } } async function queryInsertPhotoGallery(db: ClientBase, item: any): Promise<Array<PhotoModel>> { try { const queryResult = await db.query( ` INSERT INTO gallery_photo ( tenant_uuid, gallery_photo_pathfile, created_at, VALUES ${item} RETURNING gallery_photo_id, gallery_photo_pathfile ` ); if (queryResult.rowCount === 0) { return []; } return queryResult.rows; } catch (e) { console.error('[ERROR-QUERY] - func: queryInsertPhotoGallery', e); throw Error(code.database_error); } } async function queryInsertMappingPhoto(db: ClientBase, item: any): Promise<Array<PhotoModel>> { try { const queryResult = await db.query( ` INSERT INTO gallery_mapping_photo ( tenant_uuid, gallery_photo_id, gallery_album_id, created_at) VALUES ${item} RETURNING gallery_mapping_photo_id, gallery_photo_id, gallery_album_id` ); if (queryResult.rowCount === 0) { return []; } return queryResult.rows; } catch (e) { console.error('[ERROR-QUERY] - func: queryInsertPhotoGallery', e); throw Error(code.database_error); } } /** * * @param req * @param res */ export default async function createAlbum(req: Request, res: Response): Promise<any> { try { const auth = getAuth(req); const body: AlbumModel = req.body; const album = { gallery_album: null, publish_dashboard: null, publish_gallery: null, gallery_photos: [], mapping_photos: [], }; const galleryAlbum = await queryInsertGalleryAlbum(req.namespace.db, auth, body); album.gallery_album = galleryAlbum; let publishDashboard = null; if (body.published_on_dashboard) { publishDashboard = await queryInsertPublishPhotoDashboard( req.namespace.db, auth, galleryAlbum.gallery_album_id, body ); album.publish_dashboard = publishDashboard; } let publishGallery = null; if (body.published_on_gallery) { publishGallery = await queryInsertPublishPhotoGallery( req.namespace.db, auth, galleryAlbum.gallery_album_id, body ); album.publish_gallery = publishGallery; } const now = new Date(); now.setHours(now.getHours() + 7); if (body.photos.length > 0) { const photosToInsert = []; for (const photo of body.photos) { photo.s3 = s3GetPreSignedUrl( auth.tenant_uuid, 'gallery/albums', photo.gallery_photo_pathfile ); const path = photo.s3; const itemData = { tenant_uuid: auth.tenant_uuid, gallery_photo_pathfile: path.path, created_at: now.toISOString(), }; const photoItem = `( '${itemData.tenant_uuid}', '${itemData.gallery_photo_pathfile}', '${itemData.created_at}' )`; photosToInsert.push(photoItem); } const insertedPhotos = await queryInsertPhotoGallery( req.namespace.db, photosToInsert.join(', ') ); for (let i = 0; i < body.photos.length; i++) { insertedPhotos[i].s3 = body.photos[i].s3; } album.gallery_photos = insertedPhotos; } if (album.gallery_photos.length > 0) { const mappingToInsert = []; for (const photo of album.gallery_photos) { const itemData = { tenant_uuid: auth.tenant_uuid, gallery_photo_id: +photo.gallery_photo_id, gallery_album_id: +galleryAlbum.gallery_album_id, created_at: now.toISOString(), }; const mappingItem = `( '${itemData.tenant_uuid}', ${itemData.gallery_photo_id}, ${itemData.gallery_album_id}, '${itemData.created_at}' )`; mappingToInsert.push(mappingItem); } const insertedMapping = await queryInsertMappingPhoto( req.namespace.db, mappingToInsert.join(', ') ); album.mapping_photos = insertedMapping; } success(res, { status: true, version: req.version, message: 'success', data: album, meta: null, }); } catch (e) { failure(res, e); } }
Editor is loading...