Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SAP project ID validation #409

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions backend/src/components/project/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UpsertProject, projectIdSchema } from '@shared/schema/project/base';
import { User } from '@shared/schema/user';

import { codeIdFragment } from '../code';
import { sapProjectExists } from '../sap/dataImport';

async function upsertBaseProject(
tx: DatabaseTransactionConnection,
Expand Down Expand Up @@ -94,6 +95,12 @@ export async function validateUpsertProject(
) {
const validationErrors: FormErrors<UpsertProject> = { errors: {} };

if (values?.sapProjectId) {
if (!(await sapProjectExists(values.sapProjectId))) {
validationErrors.errors['sapProjectId'] = fieldError('project.error.sapProjectNotFound');
}
}

if (values?.id) {
const estimateRange = await getPool().maybeOne(sql.untyped`
SELECT
Expand Down
7 changes: 7 additions & 0 deletions backend/src/components/sap/dataImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,10 @@ export async function getSapActuals(sapProjectId: string, year: string) {
return maybeCacheSapActuals(sapProjectId, year, actuals);
}
}

export async function sapProjectExists(projectId: string) {
const wsClient = ProjectInfoService.getClient();
const [wsResult] = await wsClient.SI_ZPS_WS_GET_PROJECT_INFOAsync({ PROJECT: projectId });
const containsErrorItem = wsResult.MESSAGES?.item?.some((item: any) => item.TYPE === 'E');
return !containsErrorItem;
}
1 change: 0 additions & 1 deletion backend/src/router/project/detailplan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from '@backend/components/project/detailplan';
import { startSendMailJob } from '@backend/components/taskQueue/mailQueue';
import { getUser } from '@backend/components/user';
import { getPool, sql } from '@backend/db';
import { env } from '@backend/env';
import { TRPC } from '@backend/router';

Expand Down
76 changes: 57 additions & 19 deletions docker/sap-mock/mock-server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
(defn random-project-data [project-id]
(let [;; decode mock data parameters from the id part
;; e.g. I1112_34567
;; -> 3 WBS elements, 4 activities per WBS
;; -> 3 WBS elements, 4 activities per WBS
[_ id] (str/split project-id #"_")
[wbs-count activity-count] (take 2 id)
wbs-count (- (int wbs-count) 48)
Expand Down Expand Up @@ -334,9 +334,43 @@
[:BEKNZ BEKNZ]])
actuals)])

(def not-found-response
[[:MESSAGES [:item
[:TYPE "E"]
[:ID]
[:NUMBER "000"]
[:MESSAGE "Projektia ei löydy"]
[:LOG_NO]
[:MESSAGE_V1]
[:MESSAGE_V2]
[:MESSAGE_V3]
[:MESSAGE_V4]
[:PARAMETER]
[:ROW]
[:FIELD]
[:SYSTEM]]]
[:PROJECT_INFO
[:PSPNR "00000000"]
[:PSPID]
[:POST1]
[:ERNAM]
[:ERDAT "0000-00-00"]
[:AENAM]
[:AEDAT]
[:VERNR "00000000"]
[:VERNA]
[:ASTNR "00000000"]
[:ASTNA]
[:VBUKR]
[:PRCTR]
[:PLFAZ "0000-00-00"]
[:PLSEZ "0000-00-00"]
[:WERKS]
[:WBS]]])

;;
;; XML utils
;;
;;

(defn tag-name [tag]
(some-> tag name keyword))
Expand All @@ -352,12 +386,15 @@
(some? tag)
{tag (apply merge (map preprocess-elem content))})))

(defn ->soap-envelope [body]
(xml/indent-str
(xml/sexp-as-element
[:SOAP:Envelope
{:xmlns:SOAP "http://schemas.xmlsoap.org/soap/envelope/"}
[:SOAP:Body body]])))
(defn ->soap-response [operation & body]
(let [operation-kw (keyword (str "rfc:" (name operation) ".Response"))]
(xml/indent-str
(xml/sexp-as-element
[:SOAP:Envelope
{:xmlns:SOAP "http://schemas.xmlsoap.org/soap/envelope/"}
[:SOAP:Body [operation-kw
{:xmlns:rfc "urn:sap-com:document:sap:rfc:functions"}
body]]]))))

;;
;; SOAP endpoints
Expand All @@ -376,15 +413,17 @@
project-id (get-in root-elem [:Envelope :Body :ZPS_WS_GET_PROJECT_INFO :PROJECT])
[_ id] (str/split project-id #"_")
_ (set-seed! (Integer/parseInt id))
project (random-project-data project-id)]
project (random-project-data project-id)
not-found? (= id "404")
make-response (partial apply ->soap-response :ZPS_WS_GET_PROJECT_INFO)]
(s/validate ProjectInfoSoapMessage root-elem)
{:status 200
:content-type "text/xml"
:body (->soap-envelope
[:rfc:ZPS_WS_GET_PROJECT_INFO.Response
{:xmlns:rfc "urn:sap-com:document:sap:rfc:functions"}
[:MESSAGES]
(generate-project-data project)])})))
:body (make-response
(if not-found?
not-found-response
[[:MESSAGES]
(generate-project-data project)]))})))

(s/defschema ActualsSoapMessage
{:Envelope
Expand Down Expand Up @@ -423,11 +462,10 @@
(filter-by-date-range query-date-range))]
{:status 200
:content-type "text/xml"
:body (->soap-envelope
[:rfc:ZPS_WS_GET_ACTUALS.Response
{:xmlns:rfc "urn:sap-com:document:sap:rfc:functions"}
[:MESSAGES]
(generate-actuals-data actuals)])}))))
:body (->soap-response
:ZPS_WS_GET_ACTUALS
[:MESSAGES]
(generate-actuals-data actuals))}))))

;;
;; Web server
Expand Down
1 change: 1 addition & 0 deletions shared/src/language/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"project.error.endDateBeforeStartDate": "Loppuajankohdan oltava alkuajankohdan jälkeen",
"project.error.existingProjectObjectWBS": "Hankkeen kohteilla rakenneosia",
"project.error.costEstimateNotIncluded": "Kustannusarvioita ajankohdan ulkopuolella",
"project.error.sapProjectNotFound": "SAP-projektia ei löytynyt annetulla ID:llä",
"projectForm.saveBtnLabel": "Tallenna",
"projectForm.editBtnLabel": "Muokkaa",
"projectForm.undoBtnLabel": "Peruuta muutokset",
Expand Down