Skip to content

Commit

Permalink
Merge pull request #45 from cap-js/script-tasks
Browse files Browse the repository at this point in the history
attachments in seperate folder
  • Loading branch information
RamIndia authored Mar 1, 2024
2 parents e01826d + d7effeb commit fab4e75
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/incidents/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</style>
<script
id="sap-ui-bootstrap"
src="https://ui5.sap.com/1.120.7/resources/sap-ui-core.js"
src="https://ui5.sap.com/1.121.0/resources/sap-ui-core.js"
data-sap-ui-theme="sap_horizon"
data-sap-ui-resourceroots='{
"ns.incidents": "./"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"test": "npx jest --silent",
"add-change-tracking": "npm add @cap-js/change-tracking && cp xmpls/change-tracking.cds ./srv && cp xmpls/change-tracking.test.js ./test",
"add-telemetry": "npm add @cap-js/telemetry",
"add-attachments": "npm add @cap-js/attachments && cp xmpls/attachments.cds ./srv && cp -r xmpls/content ./db/data/content",
"clone-add-attachments": "git clone https://github.com/cap-js/attachments.git && cp -r attachments/xmpl/db . && cp -r attachments/xmpl/app . && npm add https://github.com/cap-js/attachments.git",
"clone-and-add-attachments": "git clone https://github.com/cap-js/attachments.git && npm add ./attachments && npm run add-attachments",
"add-attachments": "cp -r xmpls/attachments/ ./",
"add-notifications": "npm add @cap-js/notifications && cp xmpls/alert-notifications.js ./srv && cp xmpls/notification-types.json ./srv",
"add-audit-log": "npm add @cap-js/audit-logging && cp xmpls/data-privacy.cds ./srv && cp xmpls/audit-log.test.js ./test",
"add-remote-service": "cp -r xmpls/remote-service/ .",
Expand Down
7 changes: 0 additions & 7 deletions xmpls/attachments.cds

This file was deleted.

6 changes: 6 additions & 0 deletions xmpls/attachments/app/avatars.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using from './services';
using from '../db/attachments';

annotate ProcessorService.Incidents with @(UI.HeaderInfo: {
TypeImageUrl: customer.avatar.url
});
11 changes: 11 additions & 0 deletions xmpls/attachments/db/attachments.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using { sap.capire.incidents as my } from './schema';
using { Image, Attachments } from '@cap-js/attachments';

extend my.Incidents with {
attachments: Composition of many Attachments;
}

extend my.Customers with {
avatar: Image;
}
File renamed without changes
File renamed without changes
Binary file added xmpls/attachments/db/content/Daniel Watts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added xmpls/attachments/db/content/Stormy Weathers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added xmpls/attachments/db/content/Sunny Sunshine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
up__ID ,content ,filename ,mimeType ,note ,createdAt ,createdBy
3b23bb4b-4ac7-4a24-ac02-aa10cabd842c ,file: ,INVERTER FAULT REPORT.pdf ,application/pdf , ,2023-10-10T18:53:26.751Z ,alice
3b23bb4b-4ac7-4a24-ac02-aa10cabd842c ,file: ,Inverter-error-logs.txt ,text/plain ,Raw error logs ,2023-10-11T19:52:26.751Z ,alice
3a4ede72-244a-4f5f-8efa-b17e032d01ee ,file: ,No_Current.xlsx ,application/vnd.ms-excel , ,2023-10-09T18:50:20.751Z ,alice
3ccf474c-3881-44b7-99fb-59a2a4668418 ,file: ,strange-noise.csv ,text/csv , ,2023-10-18T18:55:26.751Z ,alice
3583f982-d7df-4aad-ab26-301d4a157cd7 ,file: ,Broken Solar Panel.jpg ,image/jpeg , ,2023-10-01T15:53:26.751Z ,alice
4 changes: 4 additions & 0 deletions xmpls/attachments/db/data/sap.common-Images.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ID ,content ,filename ,mimeType
1004155 ,file:,Daniel Watts.png ,image/png
1004161 ,file:,Stormy Weathers.png ,image/png
1004100 ,file:,Sunny Sunshine.png ,image/png
38 changes: 38 additions & 0 deletions xmpls/attachments/db/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const cds = require('@sap/cds/lib')
module.exports = async function () {

// this ensures customers are in the db already
cds.once('served', async () => {
const { 'sap.capire.incidents.Customers': Customers } = cds.model.entities
await UPDATE (Customers) .set ('avatar_ID = ID')
})

const attachments = await cds.connect.to('attachments')
const { join } = cds.utils.path
const { createReadStream } = cds.utils.fs

const { 'sap.capire.incidents.Incidents.attachments': Attachments } = cds.model.entities
await attachments.put (Attachments, [
[ '3b23bb4b-4ac7-4a24-ac02-aa10cabd842c', 'INVERTER FAULT REPORT.pdf', 'application/pdf' ],
[ '3b23bb4b-4ac7-4a24-ac02-aa10cabd842c', 'Inverter-error-logs.txt', 'application/txt' ],
[ '3a4ede72-244a-4f5f-8efa-b17e032d01ee', 'No_Current.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ],
[ '3ccf474c-3881-44b7-99fb-59a2a4668418', 'strange-noise.csv', 'text/csv' ],
[ '3583f982-d7df-4aad-ab26-301d4a157cd7', 'Broken Solar Panel.jpg', 'image/jpeg' ]
].map(([ up__ID, filename, mimeType ]) => ({
up__ID, filename, mimeType,
content: createReadStream (join(__dirname, 'content', filename)),
createdAt: new Date (Date.now() - Math.random() * 30*24*60*60*1000),
createdBy: 'alice',
})))

const { 'sap.common.Images': Images } = cds.model.entities
await attachments.put (Images, [
[ '1004155', 'Daniel Watts.png', 'image/png' ],
[ '1004161', 'Stormy Weathers.png', 'image/png' ],
[ '1004100', 'Sunny Sunshine.png', 'image/png' ]
].map(([ ID, filename, mimeType ]) => ({
ID, filename, mimeType,
content: createReadStream (join(__dirname, 'content', filename)),
})))

}

0 comments on commit fab4e75

Please sign in to comment.