1- import { storeJson } from '../fileModels/store.json'
1+ import { configJson } from '../fileModels/config.json'
2+ import { systemSmtpJson } from '../fileModels/systemSmtp.json'
23import { sdk } from '../sdk'
34
45const { InputSpec } = sdk
@@ -25,10 +26,64 @@ export const manageSmtp = sdk.Action.withInput(
2526 inputSpec ,
2627
2728 // optionally pre-fill the input form
28- async ( { effects } ) => ( {
29- smtp : ( await storeJson . read ( ( s ) => s . smtp ) . once ( ) ) || undefined ,
30- } ) ,
29+ async ( { effects } ) => {
30+ const smtp = await systemSmtpJson . read ( ) . once ( )
31+
32+ if ( smtp ?. enabled ) {
33+ return {
34+ smtp : {
35+ selection : 'system' as const ,
36+ value : { customFrom : smtp . customFrom || undefined } ,
37+ } ,
38+ }
39+ }
40+
41+ const config = await configJson . read ( ) . once ( )
42+
43+ if ( config ?. smtp_host ) {
44+ const { smtp_host, smtp_port, smtp_from, smtp_username, smtp_password } =
45+ config
46+ return {
47+ smtp : {
48+ selection : 'custom' as const ,
49+ value : {
50+ server : smtp_host ,
51+ port : smtp_port ,
52+ from : smtp_from ,
53+ login : smtp_username ,
54+ password : smtp_password ,
55+ } ,
56+ } ,
57+ }
58+ }
59+
60+ return { smtp : { selection : 'disabled' as const , value : { } } }
61+ } ,
3162
3263 // the execution function
33- async ( { effects, input } ) => storeJson . merge ( effects , { smtp : input . smtp } ) ,
64+ async ( { effects, input } ) => {
65+ if ( input . smtp . selection === 'system' ) {
66+ await systemSmtpJson . merge ( effects , {
67+ enabled : true ,
68+ customFrom : input . smtp . value . customFrom ,
69+ } )
70+ } else if ( input . smtp . selection === 'custom' ) {
71+ const { server, port, from, login, password } = input . smtp . value
72+ await configJson . merge ( effects , {
73+ smtp_host : server ,
74+ smtp_port : port ,
75+ smtp_from : from ,
76+ smtp_username : login ,
77+ smtp_password : password || undefined ,
78+ } )
79+ } else {
80+ await configJson . merge ( effects , {
81+ smtp_host : undefined ,
82+ smtp_port : undefined ,
83+ smtp_from : undefined ,
84+ smtp_username : undefined ,
85+ smtp_password : undefined ,
86+ } )
87+ }
88+ } ,
3489)
0 commit comments